最近到了新公司,leader让我研究一下WCF的传输安全机制。以前也做过WCF的应用,但是很少涉及安全方面的东西。所以,花了三天的时间研究了一下如何在WCF的应用程序中配置安全。在这个系列文章中,我会将我的一些心得分享给大家,希望对大家有所帮助。

  我所做的研究主要是使用UserName的验证方式,对请求进行验证,本文主要介绍使用basicHttpBinding进行验证,后续两篇文章中将针对wsHttpBinding介绍使用http方式验证以及https(SSL)方式验证。

  首先,想跟大家说明,这几篇帖子,我的重点在于如何配置WCF应用程序,使你的WCF程序能够使用UserName的安全验证;而不在于如何建立WCF应用程序,如果大家想了解关于建立WCF应用程序的相关知识,请参考MSDN相关文档。

  Let's go!

  我将我的应用程序分为服务端与访问端,下面让我们分别看服务端与客户端。

1. 服务端

  (1) 创建CustomUserNameValidator

  CustomUserNameValidator从UserNamePasswordValidator类继承,并且需要实现抽象方法Validate,可以在其中实现用户名密码验证逻辑,如果验证不成功,抛出异常。

 

 public class CustomUserNameValidator : UserNamePasswordValidator
{
  private const string USERNAME_ELEMENT_NAME = "userName"; private const string PASSWORD_ELEMENT_NAME = "password"; private const string FAULT_EXCEPTION_MESSAGE = "UserName or Password is incorrect!"; public override void Validate(string userName, string password)
{
  Guarder.Guard.ArgumentNotNull(userName)
      .ArgumentNotNull(password);
var validateUserName = ConfigurationManager.AppSettings[USERNAME_ELEMENT_NAME];
var validatePassword = ConfigurationManager.AppSettings[PASSWORD_ELEMENT_NAME];
var validateCondition = userName.Equals(validateUserName) && password.Equals(validatePassword);
if (!validateCondition)
{
  throw new FaultException(FAULT_EXCEPTION_MESSAGE);
}
}
}

  (2) 完成服务端配置文件

  服务端配置文件:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="customBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="EmployeesHost.CustomUserNameValidator, EmployeesHost"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings >
<basicHttpBinding>
<binding name="EmployeeQueryBinding_BasicHttp">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="EmployeesHost.EmployeesQueryService" behaviorConfiguration="customBehavior">
<!--For basic http binding endpoint-->
<endpoint address="http://127.0.0.1:12215/EmployeeQuery" binding="basicHttpBinding"
bindingConfiguration="EmployeeQueryBinding_BasicHttp"
contract="EmployeesHost.IEmployeesQueryService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8733/Design_Time_Addresses/EmployeesHost/EmployeesQueryService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<appSettings>
<add key="userName" value="username"/>
<add key="password" value="password"/>
</appSettings>
</configuration>

  需要注意的是,serviceCredentials节点中需要按照上面文件中的写法。binding节点中也需要严格按照上面文件中所写。、

  (3) 完成ServiceHost启动

  启动ServiceHost代码:

 static void Main(string[] args)
{
var host = new ServiceHost(typeof(EmployeesQueryService));
host.Open();
Console.WriteLine("Service Host opened, press <s> to stop...");
var key = Console.ReadKey();
if (key.Key == ConsoleKey.S)
{
  host.Close();
}
Console.WriteLine("Press any key to quit...");
Console.ReadKey();
14 }

2. 客户端

  (1) 客户端配置文件:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DefaultBinding_IEmployeesQueryService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<!--For basic http binding endpoint-->
<endpoint address="http://127.0.0.1:12215/EmployeeQuery/"
binding="basicHttpBinding" bindingConfiguration="DefaultBinding_IEmployeesQueryService"
contract="IEmployeesQueryService" name="DefaultBinding_IEmployeesQueryService_IEmployeesQueryService" />
</client>
</system.serviceModel>
<appSettings>
<add key="userName" value="username"/>
<add key="password" value="password"/>
</appSettings>
</configuration>

  注意,客户端配置文件中的binding节点,需要与服务端的binding节点写法相同。

  (2) 客户端调用代码

 var userName = ConfigurationManager.AppSettings[USERNAME_ELEMENT_NAME];
var password = ConfigurationManager.AppSettings[PASSWORD_ELEMENT_NAME];
mProxy.ClientCredentials.UserName.UserName = userName;
mProxy.ClientCredentials.UserName.Password = password;
var data = mProxy.GetData();

OK,大功告成,运行,ok。

下一篇:[WCF安全2]使用wsHttpBinding构建UserName授权的WCF应用程序,非SSL

[WCF安全1]使用basicHttpBinding构建UserName授权的WCF应用程序的更多相关文章

  1. [WCF安全2]使用wsHttpBinding构建UserName授权的WCF应用程序,非SSL

    上一篇文章中介绍了如何使用basicHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建非SSL的UserName安全授权的WCF应用程 ...

  2. [WCF安全3]使用wsHttpBinding构建基于SSL与UserName授权的WCF应用程序

    上一篇文章中介绍了如何使用wsHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建基于SSL的UserName安全授权的WCF应用程序. ...

  3. 构建RESTful风格的WCF服务

    构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...

  4. WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[实现篇]

    原文:WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[实现篇] 元数据的导出就是实现从ServiceEndpoint对象向MetadataSet对象转换的过程,在WCF元数据框 ...

  5. WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇]

    原文:WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇] 在[上篇]中,我们分别站在消息交换和编程的角度介绍了SOAP Fault和FaultException异常.在服务执行过 ...

  6. WCF开发实战系列一:创建第一个WCF服务

    WCF开发实战系列一:创建第一个WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 在这个实战中我们将使用DataContract,ServiceContract ...

  7. WCF开发实战系列二:使用IIS发布WCF服务

    WCF开发实战系列二:使用IIS发布WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 上一篇中,我们创建了一个简单的WCF服务,在测试的时候,我们使用VS200 ...

  8. 利用ant脚本 自动构建svn增量/全量 系统程序升级包【转】

    引文:我们公司是做自己使用产品,迭代更新周期短,每次都花费较多时间和精力打包做增量更新,发现了一篇文章用于 自动构建svn增量/全量 系统程序升级包,收藏之,希望可以通过学习,更加简化我们的工作. 文 ...

  9. 知道WCF的地址用工厂通道方式快速调用WCF

    知道WCF的地址用工厂通道方式快速调用WCF  1 using System;  2 using System.ServiceModel;  3 using System.ServiceModel.D ...

随机推荐

  1. 剑指Offer——连续子数组的最大和

    题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向 ...

  2. git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompatible or unsupported protocol version.”

    git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompat ...

  3. geo实现方案

    1.数据库内在支持GIS(地理信息系统) MySQL: 目前只有MyISAM引擎是支持GIS的,Innodb在5.7版本中才支持空间索引.MyISAM这个引擎不支持事务.外键,而且是表锁.适合读为主, ...

  4. DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False.

    DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_ ...

  5. Shiro Annotation保护实例

    之前介绍了Shiro的Web訪问控制和缓存配置,事实上Shiro和Spring一样能够使用Annotation来配置方法级别的安全.省去自己每次获取Subject去做权限验证,比方在改动用户passw ...

  6. 目标检测-Faster R-CNN

    [目标检测]Faster RCNN算法详解 Ren, Shaoqing, et al. “Faster R-CNN: Towards real-time object detection with r ...

  7. 理解tomcat之搭建简易http服务器

    做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http服务器,本篇文章将搭建一个简单的http服 ...

  8. Struts2 iterator标签实现嵌套循环

    问题:有一个List<List<Object>>  list用struts2在页面显示使用<s:iterator value=”list” var=”list1″> ...

  9. java反射子之获取方法信息(二)

    一.获取方法 1.方法作用. 2. 二.获取方法信息.(修饰符,返回值,方法名称,参数列表,抛出的异常). ############################################## ...

  10. [转]VMware-Transport(VMDB) error -44:Message.The VMware Authorization Service is not running解决方案

    转自:http://blog.sina.com.cn/s/blog_70c9c4b40101i01v.html 1.VMware Workstation中新建的虚拟机在开机的时候出现这种错误:Tran ...