服务器端与客户端都安装证书,双方通过证书加密通讯。

配置wsHttpBinding,使用基于消息的用户名验证。首先配置为Windows账户库验证。

服务器端配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<services>
<service name="Server.EchoClaims" behaviorConfiguration="echoClaimsBehavior">
<endpoint address="EchoClaims"
binding="wsHttpBinding" bindingConfiguration="echoClaimsBinding"
contract="Server.IEchoClaims"></endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="echoClaimsBinding">
<security mode="Message">
<message clientCredentialType="UserName"
negotiateServiceCredential="true"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="echoClaimsBehavior">
<serviceCredentials>
<serviceCertificate
findValue="CN=WCFServer"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectDistinguishedName"/> <userNameAuthentication userNamePasswordValidationMode="Windows"/> </serviceCredentials>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

服务器端WCF配置文件

客户端配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IEchoClaims">
<security mode="Message">
<message clientCredentialType="UserName" negotiateServiceCredential="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="echoClaimsBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:8000/EchoClaims" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IEchoClaims" contract="EchoService.IEchoClaims"
name="WSHttpBinding_IEchoClaims"
beh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4hANLTehsSe0YMfrN+8EB42BhwnYlMjFuh11k=" />
<dns value="WCFServer"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

客户端引用服务后修改配置文件

2, 自定义的成员提供程序最为身份库:
任何成员提供程序都必须派生自System.Web.MembershipProvider 基类,并且需要实现它的几个方法,以验证和管理应用程序中的用户。在WCF中只需要ValidateUser方法,用它来验证证书。

 1 namespace Server
2 {
3 public class MyMembershipProvider : UserNamePasswordValidator
4 {
5 public override void Validate(string userName, string password)
6 {
7 //throw new NotImplementedException();
8 if (userName != "joe" || password != "bar")
9 {
10 throw new SecurityTokenValidationException("The user could not be authenticated.");
11 }
12 }
13 }
14 }

自定义用户验证

 1 <?xml version="1.0" encoding="utf-8"?>
2 <configuration>
3 <startup>
4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
5 </startup>
6 <system.serviceModel>
7 <services>
8 <service name="Server.EchoClaims" behaviorConfiguration="echoClaimsBehavior">
9 <endpoint address="EchoClaims" binding="wsHttpBinding" bindingConfiguration="echoClaimsBinding" contract="Server.IEchoClaims"></endpoint>
10 </service>
11 </services>
12 <bindings>
13 <wsHttpBinding>
14 <binding name="echoClaimsBinding">
15 <security mode="Message">
16 <message clientCredentialType="UserName" negotiateServiceCredential="true"/>
17 </security>
18 </binding>
19 </wsHttpBinding>
20 </bindings>
21 <behaviors>
22 <serviceBehaviors>
23 <behavior name="echoClaimsBehavior">
24 <serviceCredentials>
25 <serviceCertificate findValue="CN=WCFServer" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectDistinguishedName"/>
26
27 <userNameAuthentication userNamePasswordValidationMode="Custom"
28 customUserNamePasswordValidatorType="Server.MyMembershipProvider, Server"/>
29
30 </serviceCredentials>
31 <serviceMetadata httpGetEnabled="true"/>
32 </behavior>
33 </serviceBehaviors>
34 </behaviors>
35 </system.serviceModel>
36
37 </configuration>

相应配置文件app.config

8.3.2 建立在传输层安全至上的用户名验证

最新文章

  1. Swift - UIView,UItableView,Cell设置边框方法
  2. 掌握VS2010调试 -- 入门指南
  3. CheckedListBoxControl 使用
  4. Lua了解 &amp; 为什么游戏开发用Lua
  5. 自定义模板语言之simple_tag和自定义过滤器
  6. ArchLinux安装开源VMware Tools
  7. ZOJ3229 Shoot the Bullet
  8. 学习Haskell的一些资料
  9. 一个AVRUSB作品HID类
  10. Js 30 BOM
  11. VUE-004-禁止修改页面显示项,设置el-input,textarea只读方法
  12. Uni2D 入门 -- Skeletal Animation
  13. JAVA动态编译(JavaCompiler)
  14. w3cscholl的在线代码编辑工具
  15. 什么是kafka以及如何搭建kafka集群?
  16. 微信小程序充值及充值回调后的处理
  17. 【定时任务】Timer
  18. Harbor 企业级 Docker Registry
  19. 50.RocketMQ (quickstart)
  20. [php]HTTP协议头解析

热门文章

  1. Css3中有关的 @media 媒体查询相关的知识
  2. 0624.python入门
  3. Qt ui_xxx.h no file or directory
  4. usbip 非源码安装
  5. ENGG1310 P2.1 Intro to CE Computer Systems, Programming &amp; Networking
  6. docker rabbitMQ安装
  7. shell_Day09
  8. VUE使用axios数据请求时报错 TypeError: Cannot set property &#39;xxxx&#39; of undefined 的解决办法
  9. es实现规格分组分析
  10. spring@Validated校验用法