相信很多开发者都用过WebService来实现程序的面向服务,本文主要介绍WebService的身份识别实现方式,当然本文会提供一个不是很完善的例子,权当抱砖引玉了.

首先我们来介绍webservice下的两种验证方式,

一.通过集成windows身份验证

通过集成windows方式解决webservice的安全问题是一个很简洁,并且行之有效的解决方案,该方案的优点是比较安全,性能较好,当然因为与windows紧密的结合到了一起,缺点自然也很明显了,第一,不便于移植,第二,要进行相关的配置部署工作(当然我们也可以用代码来操作IIS,只不过比较麻烦,最近一直做自动化部署,所以一讲到配置马上就会联想到怎么去自动部署)

具体怎么做呢?

服务器端:配置IIS虚拟目录为集成windows身份验证

客户端: Service1 wr = new Service1(); //web service实例

wr.Credentials = new NetworkCredential("administrator","123"); //用户名密码

lblTest.Text = wr.Add(2,2).ToString(); //调用Add的 web service方法

二.使用 SoapHeader(SOAP 标头)自定义身份验证

SoapHeader 多数情况下用来传递用户身份验证信息,当然它的作用远不止如此,有待于在实际应用中发掘,体可以实现哪些东西大家有想法可以留言一起交流.

SoapHeader 使用步骤:

(1) 创建继承自 System.Web.WebServices.SoapHeader 的自定义 SoapHeader 类型。

(2) 在 WebService 中创建拥有 public 访问权限的自定义 SoapHeader 字段。

(3) 在需要使用 SoapHeader 的 WebMethod 上添加 SoapHeaderAttribute 访问特性。SoapHeaderAttribute 构造必须指定 memberName 参数,就是我们在第二步中申明的字段名称。

(4) 生成器会自动为客户端生成同名的自定义 SoapHeader 类型,只不过比起我们在 WebService 端创建的要复杂一些。同时还会为代理类型添加一个 soapheaderValue 属性。

下面展示一段SoapHeader的代码,多余的方法将会在后面用到

客户端

class Program

{

static void Main(string[] args)

{

Service1 ws = new Service1();

ServiceCredential mycredential = new ServiceCredential();

mycredential.User = "gazi";

mycredential.Password="gazi";

ws.ServiceCredentialValue = mycredential;

string mystr=ws.SayHello();

}

}

服务器端

public class Service1 : System.Web.Services.WebService

{

public ServiceCredential myCredential;

[WebMethod]

[SoapHeader("myCredential", Direction = SoapHeaderDirection.In)]

public string SayHello()

{

return "hello";

}

}

public class ServiceCredential : SoapHeader

{

public string User;

public string Password;

public static bool ValideUser(string User,string Password)

{

return true;

}

public static void CheckUser(Object sender, WebServiceAuthenticationEvent e)

{

if (ValideUser(e.User, e.Password))

{

return;

}

else

{

WebServiceAuthenticationModule module = sender as WebServiceAuthenticationModule;

module.Result.AddRule("验证错误", "不能确认您的身份,请检查用户名和密码");

}

}

}

当我们拥有很多个类的时候,要添加一个或者删除一个验证方式(假设需要进行多种认证)是非常麻烦的,我们不可能跑到每个方法里面去加一个方法调用,这是灾难性的工作,当然我们也可以用AOP来实现,Aop的话需要额外增加很多代码或者直接引入第三方来做,但是我们可不可以有更简便的方法呢?

OK,答案就是使用HttpModule,我们集成IHttpModule写一个处理模块,那么它的原理是什么呢?具体进行了哪些操作呢?我们的思路如下:

HTTP Module 分析 HTTP 消息以检查它们是不是 SOAP 消息。

如果 HTTP Module 检测到 SOAP 消息,它会读取 SOAP 标头。

如果 SOAP 消息的 SOAP 标头中有身份验证凭据,HTTP Module 将引发一个自定义 global.asax 事件。

下面来看看我们的Module代码

public class WebServiceAuthenticationModule : IHttpModule

{

private static WebServiceAuthenticationEventHandler

_eventHandler = null;

///

/// 验证事件.绑定到此事件可进行对用户身份的识别

///

public static event WebServiceAuthenticationEventHandler Authenticate

{

add { _eventHandler += value; }

remove { _eventHandler -= value; }

}

public Result Result = new Result();

public void Dispose()

{

}

public void Init(HttpApplication app)

{

app.AuthenticateRequest += new

EventHandler(this.OnEnter);

Result.EndValid += new

EventHandler(this.OnCheckError);

}

///

/// 验证用户身份

///

///

private void OnAuthenticate(WebServiceAuthenticationEvent e)

{

if (_eventHandler == null)

return;

_eventHandler(this, e);

if (e.User != null)

e.Context.User = e.Principal;

}

public string ModuleName

{

get { return "WebServiceAuthentication"; }

}

void OnEnter(Object source, EventArgs eventArgs)

{

HttpApplication app = (HttpApplication)source;

HttpContext context = app.Context;

Stream HttpStream = context.Request.InputStream;

// Save the current position of stream.

long posStream = HttpStream.Position;

// If the request contains an HTTP_SOAPACTION

// header, look at this message.HTTP_SOAPACTION

if (context.Request.ServerVariables["HTTP_SOAPACTION"] == null)

return;

// Load the body of the HTTP message

// into an XML document.

XmlDocument dom = new XmlDocument();

string soapUser;

string soapPassword;

try

{

dom.Load(HttpStream);

// Reset the stream position.

HttpStream.Position = posStream;

// Bind to the Authentication header.

soapUser =

dom.GetElementsByTagName("User").Item(0).InnerText;

soapPassword =

dom.GetElementsByTagName("Password").Item(0).InnerText;

}

catch (Exception e)

{

// Reset the position of stream.

HttpStream.Position = posStream;

// Throw a SOAP exception.

XmlQualifiedName name = new

XmlQualifiedName("Load");

SoapException soapException = new SoapException(

"SOAP请求没有包含必须的身份识别信息", name, e);

throw soapException;

}

// 触发全局事件

OnAuthenticate(new WebServiceAuthenticationEvent

(context, soapUser, soapPassword));

Result.OnEndValid();

return;

}

void OnCheckError(Object sender, EventArgs e)

{

if (Result.BrokenRules.Count == 0)

{

return;

}

else

{

HttpApplication app = HttpContext.Current.ApplicationInstance;

app.CompleteRequest();

app.Context.Response.Write(Result.Error);

}

}

}

Authenticate事件是一个静态的变量,这样我们可以在程序的外部来订阅和取消订阅事件(非静态的public 事件在外部也是不能进行订阅和取消订阅事件的,这也是事件和委托的一个区别之一)

下面是我们的事件参数以及委托

public delegate void WebServiceAuthenticationEventHandler(Object sender, WebServiceAuthenticationEvent e);

///

/// 封装的事件参数

///

public class WebServiceAuthenticationEvent : EventArgs

{

private IPrincipal _IPrincipalUser;

private HttpContext _Context;

private string _User;

private string _Password;

public WebServiceAuthenticationEvent(HttpContext context)

{

_Context = context;

}

public WebServiceAuthenticationEvent(HttpContext context,

string user, string password)

{

_Context = context;

_User = user;

_Password = password;

}

public HttpContext Context

{

get { return _Context; }

}

public IPrincipal Principal

{

get { return _IPrincipalUser; }

set { _IPrincipalUser = value; }

}

public void Authenticate()

{

GenericIdentity i = new GenericIdentity(User);

this.Principal = new GenericPrincipal(i, new String[0]);

}

public void Authenticate(string[] roles)

{

GenericIdentity i = new GenericIdentity(User);

this.Principal = new GenericPrincipal(i, roles);

}

public string User

{

get { return _User; }

set { _User = value; }

}

public string Password

{

get { return _Password; }

set { _Password = value; }

}

public bool HasCredentials

{

get

{

if ((_User == null) || (_Password == null))

return false;

return true;

}

}

}

我们在Global.asax的Application_Start方法里面把前面介绍的静态方法ServiceCredential.CheckUser订阅到我们Authenticate事件上,前面提到的增加和删除多种认证方式就是通过这种方法实现的.

protected void Application_Start(object sender, EventArgs e)

{

WebServiceAuthenticationModule.Authenticate += ServiceCredential.CheckUser;

}

我们在ServiceCredential.ValideUser方法设置了返回false,这是针对测试的一个配置,实际情况下我们可以和数据库结合起来写一个认证

运行上面讲解SoapHeader的那段代码,你会发现我们的认证已经有效了.关于文章中用到的Result类改天在用一篇文章记录一下,这是一个非常好的记录错误的方案.

webservice 安全性 对外_WebService的安全性讨论【身份识别】相关推荐

  1. webservice 安全性 对外_WebService安全性的几种实现方法【身份识别】

    相信很多开发者都用过WebService来实现程序的面向服务,本文主要介绍WebService的身份识别实现方式,当然本文会提供一个不是很完善的例子,权当抱砖引玉了. 首先我们来介绍webservic ...

  2. java线程安全性_Java并发-线程安全性

    1.什么是线程安全性? 在线程安全性的定义中,最核心的就是正确性.当多线程访问调用某个类时,线程之间不会出现错误的交互,不管运行时线程如何交替执行,并且在主调代码不需要任何同步或协同,这个类都能表现出 ...

  3. Schnorr身份识别方案

    Schnorr身份识别协议是又一个零知识证明协议,相比Fiamt协议有两点不同,一是其安全性依赖于离散对数的困难性,二是该方案使用乘法群,从而可以提前计算了一些参数,减小了证明者实时计算开销,特别适合 ...

  4. 2016年安全身份识别主流趋势

    安全身份识别解决方案商HID Global通过从不同行业市场的重要客户那里所获得的深刻洞察,以及公司最新解决方案在前瞻性合作伙伴和全球各地的终端用户中进行的试点项目与正式部署情况,确定了2016年最值 ...

  5. 基于生物特征信息的身份识别(指纹、面部识别的方式和原理)的使用问题的思考

    1.应用现状及应用案例介绍 近年来,随着互联网的飞速发展,各式各样的应用也不断发展,给我们的生活带来了极大的便捷.生活中,我们日常使用我们个人的指纹以及面部信息来识别个人身份,还有虹膜技术和声纹识别技 ...

  6. 计算机用户身份识别,计算机用户身份识别装置及使用方法与流程

    本发明涉及用户身份验证技术领域,是一种计算机用户身份识别装置及使用方法. 背景技术: 随着计算机技术的不断发展,信息安全已成为计算机用户普遍关注的焦点,传统方法仅仅只是对密码内容进行确认,传统密码方式 ...

  7. 广州麦仑 全面亮相2022身份识别技术大会及第十七届SDS

    人工智能掌纹识别技术创新企业--广州麦仑信息科技有限公司确认报名参与由多维身份识别与可信认证技术国家工程研究中心.中国国际科技促进会.证件防伪公安部重点实验室.中国国际科技促进会证卡票签专业委员会联合 ...

  8. 身份识别与访问管理(IAM)

    身份识别与访问管理(IAM Identity and Access Management) 参考文章:[涨知识]认识IAM,向着"零信任"安全架构迈进 什么是身份和访问管理(IAM ...

  9. OWASP A7 Identification and Authentication Failures(身份识别和身份验证错误)

    Identification and Authentication Failures(身份识别和身份验证错误) 从 2017 的第二名下降到了 2021的第7名 我在 TOP1 中提出过水平越权和垂直 ...

  10. 移动网流量用户身份识别系统的源代码_虹膜识别系统助力疫情防控

    光明网讯日前,航天科工二院203所接到一项人员管控需求,要求在疫情防控的关键卡口.岗哨等重要位置,在人员不摘下口罩的情况下,进行人员身份甄别.203所紧急组织力量开展虹膜识别设备生产,以最快的速度将设 ...

最新文章

  1. seo说_百度指数看世间沉浮_如何快速排名-互点快速排名_网站关键词排名常见问题 - 搜狗快速排名...
  2. php7和python3性能对比-为什么PHP7执行这个简单循环比Python3快得多?
  3. 各自用一句话来概括MVC、MVP、MVVM的差异特点
  4. ASP.NET中利用ashx实现图片防盗链
  5. queuedeclare参数说明_MQ 学习笔记之RabbitMQ
  6. 微信小程序-测试游戏生成六边多边形
  7. 智慧旅游监管平台建设方案
  8. 51/52单片机 TCON控制字及TMOD寄存器
  9. simulink与gt联合仿真问题求解
  10. 振动信号的短时傅里叶变换分析
  11. leetcode347——前K个高频元素——java实现
  12. Cadence Allegro学习之铜皮(走粗线)的使用方法+罐铜+铜皮切割
  13. 有一分数序列: 2/1 3/2 5/3 8/5 13/8 21/13...... 求出这个数列的前N项之和,保留两位小数。
  14. android开发截屏代码,android截屏代码:C++实现
  15. ArrayList添加一个元素的过程(中部插入以及尾部添加)
  16. springboot gradle 打包排除依赖 排除文件
  17. 5.1 规定“学什么”的培养方案——《逆袭大学》连载
  18. 建模厉器E-RWin
  19. linux sed替换大小写,linux sed 批量替换字符串
  20. 泰山OFFICE技术讲座:逻辑字号、中文字号、保存字号、布局字号、绘制字号

热门文章

  1. 国庆记事之一:小白结婚及我跟女朋友吵架
  2. Latex WinEdt7.0查找替换功能
  3. unity C#修改脚本图标
  4. Atitit 重复文件清理工具 按照文件名 目录 1. 原理, 1 1.1. If base filex exist dele other files 1 1.2. Get getStartIdex
  5. Atitit 数据处理查询 中的异常标准化草案 jpa jdbc hb  oql规范attilax总结
  6. Atitit 自然语言处理原理与实现 attilax总结
  7. 知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31无涯 - I
  8. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
  9. paip.html 及css调试工具---debugbar
  10. 在LINUX上,Apache安装记