使用SoapHeader传递和验证用户

 

使用SoapHeader传递和验证用户

1.Web Service端的代码: 
先创建一个继承自System.Web.Services.Protocols.SoapHeader
    CredentialSoapHeader类:

public class CredentialSoapHeader : SoapHeader
 {
      private string userName; 
      private string userPassword;
 

      public string UserName 
      { 
           get { return userName ; } 
           set { userName = value ; } 
      }

      public string UserPassword 
      { 
           get { return userPassword ; } 
           set { userPassword = value ; } 
      }
  
      public CredentialSoapHeader()
      {
           this.userName     = "admin";
           this.userPassword = "admin";
      }
 }

2.创建对外的发布的WebService方法
public class MyService : System.Web.Services.WebService
 {
      public CredentialSoapHeader Credentails = 
            new CredentialSoapHeader();

      [WebMethod(Description="Welcom Test")]
      [SoapHeader("Credentails")]
      public string Welcome(string userName, string password) 
      { 
           if ((this.Credentails.UserName == userName) &&
                (this.Credentails.UserPassword == password))
               return "Welcom " + this.Credentails.UserName;
           else
               return "Lawless User";
      }
 }

3.创建客户段Clinet, 方法Login调用WebService
public class ClientForm : System.Windows.Forms.Form
 {
      private myService.MyService s = 
          new Sunways.myService.MyService();

        private void Login(string userName, string password)
          {
               string msg = s.Welcome(userName, password);
               MessageBox.Show(msg);
          }
  ......
  }

posted on 2005-04-22 12:47  高血压的熊  阅读(868)  评论(1编辑  收藏  举报