Web Service 中的身份验证策略--使用自定义SOAP 标题

自定义SOAP头验证
本文介绍了一种通过自定义SOAP头实现用户验证的方法,能够有效限制服务调用者的范围。文中详细展示了服务端验证逻辑及客户端调用示例。
自定义SOAP标题可以限制调用服务的用户范围
 1None.gifusing System;
 2None.gifusing System.Web;
 3None.gifusing System.Web.Services;
 4None.gifusing System.Web.Services.Protocols;
 5None.gif
 6None.gif[WebService(Namespace = "http://livebaby.cn")]
 7None.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 8None.gifpublic class Service : System.Web.Services.WebService
 9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
10InBlock.gif    public SecurityHeader currentUser;
11InBlock.gif    public Service()
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif
14InBlock.gif        //如果使用设计的组件,请取消注释以下行 
15InBlock.gif        //InitializeComponent(); 
16ExpandedSubBlockEnd.gif    }

17InBlock.gif    [WebMethod, SoapHeader("currentUser")]
18InBlock.gif    public string GetResult(string queryString)
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20InBlock.gif        if(ValidateUser(currentUser.UserName,currentUser.UserPass))
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            return "你发送的字符串是:"+queryString;
23ExpandedSubBlockEnd.gif        }

24InBlock.gif        else
25InBlock.gif            return "对不起:" + currentUser.UserName+",您不是合法的用户!";
26ExpandedSubBlockEnd.gif    }

27InBlock.gif    //检验SOAP HEADER 
28InBlock.gif    private bool ValidateUser(string user, string pass)
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30InBlock.gif        if (user.Equals("user"&& pass.Equals("user"))
31InBlock.gif            return true;
32InBlock.gif        else
33InBlock.gif            return false;
34ExpandedSubBlockEnd.gif    }

35ExpandedBlockEnd.gif}

36None.gif//自定义Soap Header Class
37None.gifpublic class SecurityHeader : System.Web.Services.Protocols.SoapHeader
38ExpandedBlockStart.gifContractedBlock.gifdot.gif{
39InBlock.gif    public string UserName;
40InBlock.gif    public string UserPass;
41ExpandedBlockEnd.gif}

下面是客户端的调用
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Text;
 7None.gifusing System.Windows.Forms;
 8None.gif
 9None.gifnamespace SoapHeader
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11InBlock.gif    public partial class Form1 : Form
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public Form1()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            InitializeComponent();
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18InBlock.gif        private void button_Invoke_Click(object sender, EventArgs e)
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            SoapHeader.localhost.SecurityHeader header = new SoapHeader.localhost.SecurityHeader();
21InBlock.gif            header.UserName = textBox_User.Text;
22InBlock.gif            header.UserPass = textBox_Pass.Text;
23InBlock.gif            SoapHeader.localhost.Service service = new SoapHeader.localhost.Service();
24InBlock.gif            service.SecurityHeaderValue = header;
25InBlock.gif            this.textBox_Output.Text+=service.GetResult(this.textBox_Input.Text)+Environment.NewLine;
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28ExpandedBlockEnd.gif}

29None.gif

转载于:https://www.cnblogs.com/BoKeRen/archive/2007/05/24/758458.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值