微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过SoapHeader来增强webService 的安全性

1。定义自己的soapheader的派生类

///<summary>

///定义自己的soapheader的派生类

///</summary>

    public class Mysoapheader : System.Web.Services.Protocols.soapHeader
    {
      private string _UserID = string.Empty;
      private string _PassWord = string.Empty;
      public Mysoapheader()
      { }
      public Mysoapheader(string nUserID,string nPasswWord)
      { Initial(nUserID,nPasswWord); 
      }
      public string UserID
      { get { return _UserID; }
          set { _UserID = value; }
      }
      public string PassWord
      {
          get { return _PassWord; }
          set { _PassWord = value; }
      }
      readonly
       private void Initial(string nUserID,string nPasswWord)
      {
          UserID = nUserID;
          PassWord = nPasswWord;
      }
      private bool IsValid(string nUserID,string npassWord,out string nMsg)
      {
          nMsg = "";
          try
          {
              if(nUserID == "admin" && npassWord == "admin")
              {
                  return true;
              }
              else
              {
                  nMsg = "对不起你无权此Web服务。";
                  return false;
              }
          }
          catch
          {
              nMsg = "对不起你无权此Web服务。";
              return false;
          }
      }
      public bool IsValid(out string nMsg)
      {
          return IsValid(_UserID,_PassWord,out nMsg);
      }
    }


2.添加给予SoapHeader 验证的WebService接口方法

 /// <summary>
    /// Summary description for DownloadOrders
    /// </summary>
    [WebService(Namespace = "http://shop.jnby.com",Name="订单信息")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolBoxItem(false)]
    // To allow this Web Service to be called from script,using ASP.NET AJAX,uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class DownloadOrders : System.Web.Services.WebService
    {

      //声明Soap头实例

        public Mysoapheader myHeader=new Mysoapheader();


            [SoapHeader("myHeader")]
            [WebMethod(Description = "获取退货单及退货单列表")]
            public List<Order2> GetReturnorderItems( DateTime time,string valilate)
           {
             return null;


           }

}

3.客户端调用

service1.MySoapHeader header=new service1.MySoapHeader();

header.PassWord="admin";

header.UserID="admin";

service.MySoapHeaderValue=header;

 IList<Order2> order=service.GetReturnorderItems("","");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐