使用soap的header进行身份验证

新建一个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();


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田猿笔记

写文章不容易,希望大家小小打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值