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

配置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 建立在传输层安全至上的用户名验证

WCF 安全的更多相关文章

  1. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  2. 【WCF】使用“用户名/密码”验证的合理方法

    我不敢说俺的方法是最佳方案,反正这世界上很多东西都是变动的,正像老子所说的——“反(返)者,道之动”.以往看到有些文章中说,为每个客户端安装证书嫌麻烦,就直接采用把用户名和密码塞在SOAP头中发送,然 ...

  3. 【WCF】错误协定声明

    在上一篇烂文中,老周给大伙伴们介绍了 IErrorHandler 接口的使用,今天,老周补充一个错误处理的知识点——错误协定. 错误协定与IErrorHandler接口不同,大伙伴们应该记得,上回我们 ...

  4. 【WCF】自定义错误处理(IErrorHandler接口的用法)

    当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...

  5. [WCF]缺少一行代码引发的血案

    这是今天作项目支持的发现的一个关于WCF的问题,虽然最终我只是添加了一行代码就解决了这个问题,但是整个纠错过程是痛苦的,甚至最终发现这个问题都具有偶然性.具体来说,这是一个关于如何自动为服务接口(契约 ...

  6. 【原创经验分享】WCF之消息队列

    最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...

  7. Ajax使用WCF实现小票pos机打印源码

    通过ajax跨域方式调用WCF服务,实现小票pos机的打印,源码提供web方式,客户端方式测试,服务驻留右侧底部任务栏,可控制服务开启暂停,用户可自定义小票打印模板,配合零售录入. qq  22945 ...

  8. C# 用SoapUI调试WCF服务接口(WCF中包含用户名密码的验证)

    问题描述: 一般调试wcf程序可以直接建一个单元测试,直接调接口. 但是,这次,我还要测试在接口内的代码中看接收到的用户名密码是否正确,所以,单一的直接调用接口方法行不通, 然后就想办法通过soapU ...

  9. WCF基础

    初入职场,开始接触C#,开始接触WCF,那么从头开始学习吧,边学边补充. SOA Service-Oriented Architecture,面向服务架构,粗粒度.开放式.松耦合的服务结构,将应用程序 ...

  10. Mono下的WCF的Bug?

    最近一段时间,一直在折腾Mono,折腾Linux.让我无比痛苦的是Mono下的WCF的坑真的是太多了,这不又遇到了一个莫名其妙的问题. 环境:mono 3.2.1,Jexus 5.4.3,OS Cen ...

随机推荐

  1. abap链接My sql

    Linux中ODBC源的建立--MySQL 需要用到数据库,但我用的是Linux,只能在下面安装ODBC和MYSQL     1.安装必要的包         安装ODBC     sudo apt- ...

  2. 打卡ts day01 数据类型,类

    一,环境 1 新增的数据类型和部分语法,没有办法在浏览器和node 中执行,需要安装typescript 环境 安装:npm i -g typescript 成功:tsc -v 2 在浏览器中使用ts ...

  3. error Unnecessary return statement no-useless-return

    语法错误 原本是 addUser() {       this.$refs.addFormRef.validate((valid) => {         if (!valid) return ...

  4. golang 映射(map)

    1. 映射的定义 map是一种无序的基于key-value的数据结构,Go语言中map是引用类型,必须初始化(make)才能使用. map定义: map[KeyType]ValueType 其中,Ke ...

  5. 通过pxelinux引导管理器网络引导slax linux

    前提条件 1.用于引导服务器具备tftp服务和http服务: 2.使用syslinux项目下lpxelinux.0做为pxe引导文件,原因:测试发现lpxelinux.0对http协议支持更好,可以更 ...

  6. VS2015使用gitee插件进行协同开发

    众所周知,软件项目壮大后是需要安装git,svn这些协同软件来进行合作开发的.最近接了一位电气工程师的活,让我帮忙开发一套上位机,这位工程师年数已高,仍热心学习编程每日至深夜.于是乎,我们商讨之下便决 ...

  7. 题解[LuoguP5591]小猪佩奇学数学

    题解[LuoguP5591]小猪佩奇学数学 前置知识 基本数论推式子能力,如分配律结合律等 等比数列求和 \(\sum_{i=a}^bx^i=\dfrac{x^{b+1}-x^a}{x-1}\) 二项 ...

  8. Oracle常用的日期操作函数 to_char()和to_date

    https://www.cnblogs.com/zhangliang88/p/12924407.html

  9. ENGG1310 Electricity and electronics P1.3 Electromagnetic

    课程内容笔记,自用,不涉及任何 assignment,exam 答案 Notes for self-use, do not include any assignments or exams 这一节主要 ...

  10. 2023最新版Selenium 4.6.0语法快速入门

    简介 Selenium是一款强而有力的前端应用测试工具,也非常适合搭配Python作为网络爬虫的工具:Selenium可以模拟使用者所有浏览器操作的动作,包括输入文本.点击按钮及拖拽进度条等.有鑑于新 ...