[原创]总结WebService限制用户访问的几种方法

时间:2023-02-01 09:49:11

1.集成windows验证

 

2.soap头验证信息

  创建一个类集成SoapHeader,里面属性存放验证信息(用户名,密码等),此字段必须设为公共(public)的.

  

  
  
  
1 public class SoapUsernameAndPassword:SoapHeader
2 {
3 public string username;
4 public string password;
5 }

 

 

  在WebService程序中加入该类实例.

 

  
  
  
public SoapUsernameAndPassword suap = new SoapUsernameAndPassword();

 

给带验证的方法加入SoapHeader属性标签.

 

[原创]总结WebService限制用户访问的几种方法[原创]总结WebService限制用户访问的几种方法代码
   
   
   
1 [WebMethod]
2 [System.Web.Services.Protocols.SoapHeader( " suap " ,Direction = SoapHeaderDirection.InOut,Required = true )]
3 public string HelloWorld() {
4 if (suap.username == " admin " && suap.password == " admin " )
5 {
6 return " Hello World " ;
7 }
8 else return " Please Try a Valid User " ;
9 }