程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 使用soap的header進行身份驗證

使用soap的header進行身份驗證

編輯:.NET實例教程

新建一個webService site

1.新建一個 ValidationSoapHeader.cs

/// <summary>

/// Summary description for ePhoneCredentials

/// </summary>

public class ValidationSoapHeader : SoapHeader
{

    private string _devToken;
    private string _name;
    private string _passWord;

    public ValidationSoapHeader()
    {
    }

    public ValidationSoapHeader(string devToken)
    {

        this._devToken = devToken;

    }

    public string DevToken
    {

        get { return this._devToken; }

        set { this._devToken = value; }

    }

    public string Name
    {

        get { return this._name; }

        set { this._name = value; }

    }

    public string PassWord
    {

        get { return this._passWord; }

        set { this._passWord = value; }

    }

}

2.開始編寫服務類

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 

public class Service : System.Web.Services.WebService
{

    public ValidationSoapHeader Authentication;

    private const string DEV_TOKEN = "12345";

    public Service()
    {

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

 

    [SoapHeader("Authentication")]

    [WebMethod]

 

    public string HelloWorld()
    {

        if (Authentication != null && Authentication.DevToken == DEV_TOKEN)
        {

            return "Hello World";

        }

        else
        {

            throw new Exception("Authentication Failed");

        }

    }


    [SoapHeader("Authentication")]

    [WebMethod]

 

    public string CheckLogin()
    {

        if (Authentication != null )
        {

            if (Authentication.Name == "candu" && Authentication.PassWord == "candu")
            {
                return "ok";
            }
            else
            {
                return "error";
            }

        }

        else
        {

            throw new Exception("Authentication Failed");

        }

    }
}

3.客戶端調用

  protected void Page_Load(object sender, EventArgs e)
    {
        localhost.ValidationSoapHeader header = new localhost.ValidationSoapHeader();

        header.DevToken = "12345";
        header.Name = "candu1";
        header.PassWord = "candu";
        localhost.Service ws = new localhost.Service();
       
        ws.ValidationSoapHeaderValue = header;

        Response.Write(ws.HelloWorld());

        Response.Write("<br/>"+ws.CheckLogin());
      //  Console.ReadLine();


    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved