摘 自: http://blog.sina.com.cn/s/blog_72b7a82d0100yyp8.html

WebService基于SoapHeader实现安全认证[webservice][.net][安全][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; namespace WuFrame
{ /// <summary>
///自定义的SoapHeader
/// </summary>
public class WuSoapHeader : System.Web.Services.Protocols.SoapHeader
{
private string userName = string.Empty;
private string passWord = string.Empty;
/// <summary>
/// 构造函数
/// </summary>
public WuSoapHeader()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="passWord">密码</param>
public WuSoapHeader(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.SessionState;
using System.Web.Services; using WuFrame;
using BSFW.Dao;
using BSFW.Model;
using System.Data; namespace BSFW
{
/// <summary>
/// Test 的摘要说明
/// </summary>
[WebService(Namespace = "http://wuyf.ws/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)] public class Test : System.Web.Services.WebService
{
public WuSoapHeader myHeader = new WuSoapHeader(); [WebMethod(EnableSession = true)]
//[WebMethod]
[System.Web.Services.Protocols.SoapHeader("myHeader")]
public string HelloWorld(string name)
{
bool isLogin = false;
string loginMsg = string.Empty; if (myHeader.UserName.Equals("wu") && myHeader.PassWord.Equals(""))
{
isLogin = true;
loginMsg = "验证成功!";
}
else
{
isLogin = false;
loginMsg = "验证失败! username: " + myHeader.UserName + ", passowrd:" + myHeader.PassWord;
} if (Session["name"] == null)
{
Session.Add("name", "");
}
Session["name"] = name;
return Session["name"].ToString() + ", 验证消息: " + loginMsg;
}
}

}

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

        WsTest.Test test = new WsTest.Test();
System.Net.CookieContainer cc = new System.Net.CookieContainer();
private void button1_Click(object sender, EventArgs e)
{
WsTest.WuSoapHeader header = new WsTest.WuSoapHeader();
header.UserName = txt_User.Text.ToString();
header.PassWord = txt_Pwd.Text.ToString();
test.WuSoapHeaderValue = header; MessageBox.Show(test.HelloWorld("winformApp_" + Guid.NewGuid().ToString()));
}

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

当然可以启用 Session 来解决以上所述的问题...

WebService基于SoapHeader实现安全认证[webservice][.net][安全][soapheader]的更多相关文章

  1. WebService基于SoapHeader实现安全认证

    本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来保护Web服务,如果使用的是Viaual Studio 2008可以使 ...

  2. WebService基于SoapHeader实现安全认证(一)

    本文转载:http://www.cnblogs.com/houleixx/archive/2009/08/22/webservice-soapheader-security.html WebServi ...

  3. WebService基于SoapHeader实现安全认证(二)

    支持通过Http请求方法调用webservice,同时支持SoapHeader验证. using Globalegrow.Common; using Globalegrow.Model; using ...

  4. WebService基于soapheader的身份验证

    用WebService开发接口十分方便.但接口提供的数据不应是对所有人可见的,我们来利用SoapHeader写一个简单的身份验证Demo 目录 创建WebService项目(带SoapHeader) ...

  5. iOS开发笔记 基于wsdl2objc调用asp.net WebService

    1.准备 先下载待会要用到的工具 WSDL2ObjC-0.6.zip WSDL2ObjC-0.7-pre1.zip 我用的是WSDL2ObjC-0.6.zip 1.1搭建asp.net WebServ ...

  6. Web Server 在iis上部署webservice基于oracle

    在iis上部署webservice基于oracle 常见错误及解决方案: 原因: 先安装.netframework后安装iis造成的: 解决方案: 1.C:\Windows\Microsoft.NET ...

  7. [置顶] WebService学习总结(1)——WebService相关概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  8. SharePoint 2013 配置基于AD的Form认证

    前 言 配置SharePoint 2013基于AD的Form认证,主要有三步: 1. 修改管理中心的web.config: 2. 修改STS Application的web.config: 3. 修改 ...

  9. sharepoint:基于AD的FORM认证

    //来源:http://www.cnblogs.com/jindahao/archive/2012/05/07/2487351.html 需求: 1. 认证要基于AD 2. 登入方式要页面的方式(fo ...

随机推荐

  1. Linked List Cycle I&&II——快慢指针(II还没有完全理解)

    Linked List Cycle I Given a linked list, determine if it has a cycle in it. Follow up: Can you solve ...

  2. JAVA用POI读取和创建2003和2007版本Excel

    1.添加maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-o ...

  3. Linux下GCC相关知识点

    摘要: 总结GCC的具体使用,动态库静态库的相关问题 参考资料: <Linux网络编程> ISBN:9787302207177 p19 1 GCC简介 GCC是Linux下的编译工具集,是 ...

  4. springBoot service层 事务控制

    springBoot使用事物比较简单,在Application启动类s上添加@EnableTransactionManagement注解,然后在service层的方法上添加@Transactional ...

  5. centos6.5 安装scrapy

    1. 安装Twisted, 下载安装包 python setup.py install 2. yum install libffi-devel python-devel 3 pip install s ...

  6. 【C++】基础及引用

    输出 #include "iostream" //包含c++的头文件 //iostream.h using namespace std; //使用命名空间 std 标准的命名空间 ...

  7. SQL练习总结

    [SQL语句练习] 1. 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | ...

  8. Mixins 改成使用高阶组件调用

    把组件放在另外一个组件的 render 方法里面, 并且利用了 {...this.props} {...this.state} 这些  JSX 展开属性 对比下2种代码: 原始方式: <!DOC ...

  9. 【BZOJ 4572】【SCOI 2016】围棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=4572 轮廓线DP:设\(f(i,j,S,x,y)\). \(S\)表示\((i,1)\)到\((i, ...

  10. Codeforces 551 D. GukiZ and Binary Operations

    \(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...