customsoapheadersservice.cs

来自「Microsoft Mobile Development Handbook的代码」· CS 代码 · 共 53 行

CS
53
字号
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 + =
减小字号Ctrl + -
显示快捷键?