加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用soap的header进行身份验证

发布时间:2020-12-17 02:47:17 所属栏目:安全 来源:网络整理
导读:新建一个webService site 1.新建一个?ValidationSoapHeader.cs /// summary /// Summary description for ePhoneCredentials /// /summary public class ValidationSoapHeader : SoapHeader { ??? private string _devToken; ??? private string _name; ???

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

??? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读