Web Service 中的身分識別驗證策略–使用自訂SOAP 標題

來源:互聯網
上載者:User
自訂SOAP標題可以限制調用服務的使用者範圍 1using System;
 2using System.Web;
 3using System.Web.Services;
 4using System.Web.Services.Protocols;
 5
 6[WebService(Namespace = "http://livebaby.cn")]
 7[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 8public class Service : System.Web.Services.WebService
 9{
10    public SecurityHeader currentUser;
11    public Service()
12    {
13
14        //如果使用設計的組件,請取消注釋以下行 
15        //InitializeComponent(); 
16    }
17    [WebMethod, SoapHeader("currentUser")]
18    public string GetResult(string queryString)
19    {
20        if(ValidateUser(currentUser.UserName,currentUser.UserPass))
21        {
22            return "你發送的字串是:"+queryString;
23        }
24        else
25            return "對不起:" + currentUser.UserName+",您不是合法的使用者!";
26    }
27    //檢驗SOAP HEADER 
28    private bool ValidateUser(string user, string pass)
29    {
30        if (user.Equals("user") && pass.Equals("user"))
31            return true;
32        else
33            return false;
34    }
35}
36//自訂Soap Header Class
37public class SecurityHeader : System.Web.Services.Protocols.SoapHeader
38{
39    public string UserName;
40    public string UserPass;
41}

下面是用戶端的調用 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8
 9namespace SoapHeader
10{
11    public partial class Form1 : Form
12    {
13        public Form1()
14        {
15            InitializeComponent();
16        }
17
18        private void button_Invoke_Click(object sender, EventArgs e)
19        {
20            SoapHeader.localhost.SecurityHeader header = new SoapHeader.localhost.SecurityHeader();
21            header.UserName = textBox_User.Text;
22            header.UserPass = textBox_Pass.Text;
23            SoapHeader.localhost.Service service = new SoapHeader.localhost.Service();
24            service.SecurityHeaderValue = header;
25            this.textBox_Output.Text+=service.GetResult(this.textBox_Input.Text)+Environment.NewLine;
26        }
27    }
28}
29

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.