SoapHeader +vs2008-关于Web服务状态和安全性

因项目中有个功能。及在web服务端验证用户的角色,因为通过session方式传递。可能有安全性的隐患,故采用SoapHeader 方式。

在服务器端的service 方法加上一个继承SoapHeader 的类。

   public class Mysoapheader:System.Web.Services.Protocols.SoapHeader
    {
        public string UserName;
        public string Password;
   
    }

 

   public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        public Mysoapheader mysoaper = new Mysoapheader();//必须public

        [WebMethod]
        [SoapHeader("mysoaper")]//mysoaper必须是上面的变量
        public int Sum(int a, int b)
        {
            if (Validator())
            {
                return a + b;
            }
            else
            {
                return 0;
            }
        }


        private bool Validator()
        {
            if (mysoaper == null)
            {
                return false;
            }
            else
            {
                if (mysoaper.UserName == "aaa")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

 

在客户端调用:

    private void button1_Click(object sender, EventArgs e)
        {
            wssoaper.Mysoapheader mysoapheader = new WindowsFormsApplication1.wssoaper.Mysoapheader();
            mysoapheader.UserName = "ccc";
            mysoapheader.Password = "bbb";
            var soaper = new wssoaper.Service1SoapClient();
             MessageBox.Show(soaper.Sum(mysoapheader, 10, 8).ToString());//05和08调用有区别
        }

 

如果感觉ws安全性问题可以采用WSE:webservice 加强版

转载于:https://www.cnblogs.com/zhbsh/archive/2011/05/18/2049524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值