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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - 侯垒

中转站余额为什么掉得快?我拆了一次 AI 编程任务的真实消耗 我让 Claude 写了一个贪吃蛇游戏,然后用 ccglass 看清它发给模型的真实请求 AI 编程 Agent 不是黑箱了:用 ccglass 看清 Claude Code 和 Codex 的真实请求 软件工程--软件过程模型 UML 类图 (转)深入理解Javascript闭包(closure) 数据库中使用自增量字段与Guid字段作主键的性能对比(补充篇) SQL Server 查询处理中的各个阶段 敏捷软件开发--敏捷宣言 asp.net抛出System.Data.OleDb.OleDbException:未指定的错误 架构师 通用的数据库操作类(支持多种数据库) 数据库中使用自增量字段与Guid字段作主键的性能对比 将GridView中的数据导出到Excel中下载并且解决乱码的问题 设计模式----建造者模式(Builder Pattern) 大学生联盟开发团队需要我们共同的努力 - 侯垒 - 博客园 SQL 数据库操作类 只启动一个窗体,如果再次启动则激活该窗体 如何将自己的代码自动添加版权信息的及其扩展 如何将自己的代码自动添加版权信息 轻松学习适配器模式(Adapter Pattern) 设计模式-----桥接模式(Bridge Pattern)
WebService基于SoapHeader实现安全认证
侯垒 · 2009-08-22 · via 博客园 - 侯垒

WebService基于SoapHeader实现安全认证 

      本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来保护Web服务,如果使用的是Viaual Studio 2008可以使用WCF,WCF里面提供了更多的服务认证方法。以下提供一种基于SoapHeader的自定义验证方式。

1.首先要自定义SoapHeader,须继承System.Web.Services.Protocols.SoapHeader

using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
///自定义的SoapHeader
/// </summary>
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{

    private string userName=string.Empty;
    private string passWord=string.Empty;

    /// <summary>
    /// 构造函数
    /// </summary>
    public MySoapHeader()
    {

    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="passWord">密码</param>
    public MySoapHeader(string userName, string passWord)
    {
        this.userName = userName;
        this.passWord = passWord;
    }

    /// <summary>
    /// 获取或设置用户用户名
    /// </summary>
    public string UserName
    {
        get { return userName; }
        set { userName = value; }

    }

    /// <summary>
    /// 获取或设置用户密码
    /// </summary>
    public string PassWord
    {
        get { return passWord; }
        set { passWord = value; }
    }
}

2.添加WebService,并编写相应代码。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{

    //声明Soap头实例
    public MySoapHeader myHeader=new MySoapHeader();
 
    [System.Web.Services.Protocols.SoapHeader("myHeader")]
    [WebMethod]
    public string HelloWord()
    {
        //可以通过存储在数据库中的用户与密码来验证
        if (myHeader.UserName.Equals("houlei")&myHeader.PassWord.Equals("houlei"))
        {
            return "调用服务成功!";
        }
        else
        {
            return "对不起,您没有权限调用此服务!";
        }
    }   
}

3.客户端调用,分别使用不设置SoapHeader与设置SoapHeader。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {

            localhost.WebService service = new localhost.WebService();

            //没有设置SoapHeader的服务调用
              Console.WriteLine("没有设置SoapHeader:" + service.HelloWord());
            Console.WriteLine();

            //将用户名与密码存入SoapHeader;
            localhost.MySoapHeader header = new localhost.MySoapHeader();
            header.UserName = "houlei";
            header.PassWord = "houlei";
            service.MySoapHeaderValue = header;

            //设置SoapHeader的服务调用
              Console.WriteLine("设置SoapHeader:" + service.HelloWord());
            Console.Read();
        }
    }
}

4.运行应用程序,查看运行结果。

image

再看一下直接通过浏览器的调用结果。

image

点击HelloWord调用Web服务,结果如下:

image

点击“调用”按钮,得到从服务器返回调用结果。

image

      添加自定义SoapHeader可以成功调用WebService,否则不能调用WebService,从而实现对Web Service的非法调用。这种方法存在一定的弊端,就是在每一个WebService方法上都要进行一下验证,如果用户名与密码存储在数据库中,每调用一次WebService都要访问一次数据库进行用户名与密码的验证,对于频繁调用WebService来说,数据库压力很大。然而少量WebService调用这种方式还是一种不错的选择。