No.1 先定义一个SoapHeader
/// <summary>
/// 定义SoapHeader.
/// </summary>
public class CredentialSoapHeader : SoapHeader
{
public string Username;
public string PasswordHash;
public string SecurityKey;
}
-------------------------------------------------------
No.2 定义安全类,继承WebService。并提供验证方法。
public class SecureWebService : WebService
{
public CredentialSoapHeader Credentials;
protected string VerifyCredentials()
{
if (this.Credentials == null
|| this.Credentials.Username == null
|| this.Credentials.PasswordHash == null)
{
throw new SoapException("没有提供信任令牌",
SoapException.ClientFaultCode, "Security");
}
return CheckCredential(this.Credentials);
}
}
----------------------------------------------------------------------
No.3 让你发布的webservice继承你的安全类。
public class SecureService : SecureWebService
{
public SecureService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
}
-----------------------------------------------------------
No.4发布你的Webservice。在客户端对SoapHeader赋值,调用Webservice。完成。