📄 customsoapheadersservice.cs
字号:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// SOAPheaderService contains a Web method that requires that a
/// AuthHeader object be passed in the SOAP headers.
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SOAPHeaderService : System.Web.Services.WebService
{
// Declare a public field of type AuthHeader, which becomes
// part of the Web service contract
public AuthHeader AuthToken;
public SOAPHeaderService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod(Description =
"This method requires a custom soap header set by the caller")]
[SoapHeader("AuthToken", Direction = SoapHeaderDirection.In)]
public bool Authenticate()
{
// Check for header
if (AuthToken == null)
{
throw new Exception("AuthHeader not passed in SOAP headers");
}
// Code to authenticate a user using the AuthToken object
// in this simple example, we just look for hard coded values
// but a real application might look users up in a database or
// use some other form of authentication.
if (AuthToken.Username == "andy" && AuthToken.Password == "P455w0rd")
return true;
else
return false;
}
}
// AuthHeader class extends from SoapHeader
public class AuthHeader : SoapHeader
{
public string Username;
public string Password;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -