惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
L
LangChain Blog
WordPress大学
WordPress大学
V
V2EX
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Docker
Recent Announcements
Recent Announcements
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog

博客园 - 博客人

Asp.net2.0页面的生命周期 写有效率的SQL查询(V) aspx页面事件执行顺序 如何从优化SQL入手提高数据仓库的ETL效率(转载) 简单的3个SQL视图搞定所有SqlServer数据库字典 (转载) IIS的各种身份验证详细测试 .Net专题(C# 中的委托和事件) - Part.1 创建X509证书,并获取证书密钥的一点研究 Url重写 asp.net中数据校验部分的封装与应用 SQL Server 索引结构及其使用(一) asp.net2.0导出pdf文件完美解决方案 ASP.NET 2.0加密Web.config 配置文件 WebService中使用自定义类的解决方法(5种) Web Service Authentication + MD5 [from] MS-SQL数据库开发常用汇总和t-sql技巧集锦 [原创] 如何追踪每一笔记录的来龙去脉:一个完整的Audit Logging解决方案—Part I 通用异常处理框架 如何使用.NET配置文件(转载)
Web Service 中的身份验证策略--使用自定义SOAP 标题
博客人 · 2007-05-24 · via 博客园 - 博客人

自定义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