原文地址:

http://blog.csdn.net/zxz414644665/article/details/9308055

过程:用户调用service,服务端验证用户传来的用户名和密码(传输过程用X509证书验证及加解密),验证通过则给予调用服务。

1. 生成证书:这里只会生成一个测试用的证书(使用makecert.exe生成),生成完成之后再使用MMC Console给予相应的权限(Manage Private Keys…)。

makecert.exe -sr LocalMachine -ss My -n CN=MyServerCert -sky exchange –pe

2.Server端: 用VS2010 IDE新建一个“WCF Service Application”项目方案,实现我们自己的UserName认证方法,其实就是继承UserNamePasswordValidator(需要添加对System.IdentityModel.dll引用)并重写方法Validate。

  1. /// <summary>
  2. /// MyCustomValidator.cs
  3. /// </summary>
  4. public class MyCustomValidator: UserNamePasswordValidator
  5. {
  6. /// <summary>
  7. /// Override Validate method to implement custom validation
  8. /// </summary>
  9. /// <param name="userName">Username</param>
  10. /// <param name="password">Password</param>
  11. public override void Validate(string userName, string password)
  12. {
  13. if (string.IsNullOrEmpty(userName))
  14. {
  15. throw new ArgumentNullException("userName");
  16. }
  17. if (string.IsNullOrEmpty(password))
  18. {
  19. throw new ArgumentNullException("password");
  20. }
  21. // This is for testing purpose
  22. if (userName != "Admin" || password != "123456")
  23. {
  24. // Why we can't catch this fault exception in client
  25. FaultException fault =
  26. new FaultException(
  27. new FaultReason("UserName or password is wrong!"),
  28. new FaultCode("Error:0x0001"));
  29. throw fault;
  30. }
  31. }
  32. }

在这之后写我们的服务,很简单。

  1. [ServiceContract]
  2. public interface IValidationService
  3. {
  4. [OperationContract]
  5. string PrintMessage(string message);
  6. public class ValidationService : IValidationService
  7. {
  8. public string PrintMessage(string message)
  9. {
  10. Console.WriteLine("Message = " + message);
  11. return message;
  12. }

到这里Server端的代码基本上完成了,接下来就是如何让这些代码能够协同工作,那就需要修改我们的配置文件Web.config,有3个地方需要修改或者添加。

第一,添加binding,这里我们添加wsHttpBinding;

  1. <!-- Manually added bindings -->
  2. <bindings>
  3. <wsHttpBinding>
  4. <binding name="mySecurityBinding">
  5. <security mode="Message">
  6. <message clientCredentialType="UserName" />
  7. </security>
  8. </binding>
  9. </wsHttpBinding>
  10. </bindings>

第二,添加对UserName认证的配置,即serviceCredentials配置

  1. <!-- Manually added credentials -->
  2. <serviceCredentials>
  3. <serviceCertificate findValue="MyServerCert" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />
  4. <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFValidationServer.CustomValidation.MyCustomValidator,WCFValidationServer" />
  5. </serviceCredentials>

第三,配置必不可少的endpoint;

  1. <!-- Manually added service endpoint -->
  2. <services>
  3. <service behaviorConfiguration="WCFValidationServer.ValidationServiceBehavior" name="WCFValidationServer.ValidationService">
  4. <endpoint address="" binding="wsHttpBinding" contract="WCFValidationServer.IValidationService"
  5. bindingConfiguration="mySecurityBinding">
  6. <identity>
  7. <dns value="MyServerCert" />
  8. </identity>
  9. </endpoint>
  10. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  11. </service>
  12. </services>

至此,Server端还需要做最后一件事情,即host到IIS中,这里就不多说了,测试host成功既可。

3.    Client端:同样的,用VS2010新建一个“ASP.NET Web Application”项目方案,添加对WCF service的引用,IDE会自动添加WCF的相关配置工作,这里有一点需要注意,因为我们的证书并不是真正意义上的证书,只是测试生成的,所以这个证书并不会通过验证,所以当我们客户端访问服务的时候会报错的(具体的错误不说了,自己试了就知道),我们需要在客户端添加一个自己的X509证书验证方法,这里为了测试方面,我们重新的Validate方法是空的,即不做任何认证判断。

  1. public class MyX509Validator : X509CertificateValidator
  2. {
  3. public override void Validate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate)
  4. {
  5. }
  6. }

好了,代码完成,同样我们需要修改客户端的Web.config配置文件来让这段代码起作用,添加如下的behavior,并将client的endpoint的behaviorConfiguration设置为我们添加的。

  1. <client>
  2. <endpoint address="http://localhost:8000/ValidationService.svc"
  3. binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IValidationService"
  4. contract="WCFValidationService.IValidationService" name="WSHttpBinding_IValidationService" behaviorConfiguration="myClientBehavior">
  5. <identity>
  6. <dns value="MyServerCert" />
  7. </identity>
  8. </endpoint>
  9. </client>
  10. <behaviors>
  11. <endpointBehaviors>
  12. <behavior name="myClientBehavior">
  13. <clientCredentials>
  14. <serviceCertificate>
  15. <authentication certificateValidationMode="Custom" customCertificateValidatorType="WCFValidationClient.MyX509Validator,WCFValidationClient" />
  16. </serviceCertificate>
  17. </clientCredentials>
  18. </behavior>
  19. </endpointBehaviors>
  20. </behaviors>

好了,至此我们的整个项目就完成了。客户端的界面及调用service就不说了,可以看我上载的源代码

【转载】WCF 客户端识别认证之UserName认证的更多相关文章

  1. 实现 Web 后端和客户端之间的分布式和认证通讯

    stack.io 是一个用于实现 Web 后端和客户端之间的分布式和认证通讯. 服务器端进程之间的通讯是非常高效的,因为没有中间的代理.而来自客户端的请求通过 socket.io 进入 Node.js ...

  2. 转载——Java与WCF交互(二):WCF客户端调用Java Web Service

    在上篇< Java与WCF交互(一):Java客户端调用WCF服务>中,我介绍了自己如何使用axis2生成java客户端的悲惨经历.有同学问起使用什么协议,经初步验证,发现只有wsHttp ...

  3. 【转载】ASP.NET网站选购阿里云服务器的时候,阿里云账号个人认证以及企业认证有何不同

    在采购阿里云产品,如阿里云云服务器.阿里云短信包.阿里云数据库MySql以及Sqlserver.阿里云对象存储OSS等云产品的时候,如果账号未进行实名认证,很多时候会要求实名认证操作,在实名认证时可选 ...

  4. java https单向认证(忽略认证)并支持http基本认证

    https单向认证(忽略认证)并支持http基本认证, 温馨提示 1,jar包要导入对 2,有匿名类编译要注意 3,欢迎提问,拿走不谢!背景知识 Https访问的相关知识中,主要分为单向验证和双向验证 ...

  5. 前端学HTTP之客户端识别和cookie

    前面的话 Web服务器可能会同时与数千个不同的客户端进行对话.这些服务器通常要记录下它们在与谁交谈,而不会认为所有的请求都来自匿名的客户端.本文主要介绍客户端识别及cookie机制 HTTP首部 HT ...

  6. asp.net权限认证:摘要认证(digest authentication)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  7. 和我一起学《HTTP权威指南》——客户端识别与cookie机制

    客户端识别与cookie机制 服务器需要区别是哪个客户端. 个性化接触 HTTP是匿名.无状态的请求/响应协议. Web站点希望: 对客户端的用户有更多的了解 追踪用户浏览页面的行为 因此,产生了几种 ...

  8. [转]asp.net权限认证:摘要认证(digest authentication)

    本文转自:http://www.cnblogs.com/lanxiaoke/p/6357501.html 摘要认证简单介绍 摘要认证是对基本认证的改进,即是用摘要代替账户密码,从而防止明文传输中账户密 ...

  9. HTTP认证之摘要认证——Digest(一)

    导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 一.概述 Di ...

随机推荐

  1. MOTT介绍(2)window安装MQTT服务器和client

    MQTT目录: MQTT简单介绍 window安装MQTT服务器和client java模拟MQTT的发布,订阅 window安装MQTT服务器,我这里下载了一个apache-apollo-1.7.1 ...

  2. Spring中的统一异常处理方式

    源自:https://segmentfault.com/a/1190000016236188 在具体的SSM项目开发中,由于Controller层为处于请求处理的最顶层,再往上就是框架代码的. 因此, ...

  3. Linux chkconfig命令详解

    chkconfig命令检查.设置系统的各种服务.这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务.谨记chkconfig不是 ...

  4. Exchange & Office 365最小混合部署

    前言 这篇文章的主题是混合部署~ 混合使得本地组织和云环境像一个单一的.协作紧密的组织一样运作.当组织决定进行混合部署,达到本地Exchange Server和Office 365共存的状态时,就会面 ...

  5. Mysql--通俗易懂的左连接、右连接、内连接

    刚开始看书的时候花了好长时间理解 先通俗易懂的描述下: left join(左联接): 返回包括左表中的所有记录和右表中联结字段相等的记录. right join(右联接): 返回包括右表中的所有记录 ...

  6. October 08th 2017 Week 41st Sunday

    Talent wins games, but teamwork and intelligence wins championships. 才华让你赢得比赛,团队及智慧让你赢得冠军. But the m ...

  7. September 20th 2017 Week 38th Wednesday

    All our dreams can come true if we have the courage to pursue them. 如果我们有勇气去追求梦想,我们的梦想一定可以成为现实. If y ...

  8. 个人作业——APP案例分析

    APP--饿了么:平台--Android 第一部分 调研, 评测 1 个人上手体验 首先送上APP截图一张: 相信很多学生都用过这个APP了,第一次使用都是因为新用户有大额的满减优惠才下载这个APP使 ...

  9. 第2次作业——APP案例分析

    第一部分 调研, 评测 1.下载软件并使用起来,描述最简单直观的个人第一次上手体验. 知乎,中文互联网最大的知识平台.使用知乎这个APP3年了,目睹了它的兴盛(当然没有衰亡@_@).打开这款APP,主 ...

  10. 【译文】MySQL InnoDB 使用的锁分析

    InnoDB 使用的 锁类型 共享锁和排它锁 意向锁 记录锁 间隙锁 Next-key 锁 插入意向锁 AUTO-INC 锁 共享锁和排他锁 InnoDB实现了俩个标准的行级锁,共享锁和排它锁. 共享 ...