【转自】x.509证书在wcf中的应用( web/iis篇)

在上一篇"x.509证书在WCF中的应用(CS篇)"里,我们知道了如何在应用程序中,利用x.509证书来验证WCF的消息安全(没看过的朋友建议先看下,地址http://www.cnblogs.com/yjmyzz/archive/2008/08/20/1272550.html),这一篇我们将尝试把x.509证书放到IIS里来验证WCF。

WCF宿主在IIS和普通应用程序里,原理虽然没什么不同,但在实际测试中发现,如果服务端与客户端都采用x.509证书来验证,服务端设置的自定义验证客户端证书的方法总是不起作用,无奈之下,只能在客户端采用了一种变相的方法来验证客户端证书。

废话不多说,还是来看具体步骤吧:

开发环境: Windows2003 + VS2008(SP1)

一.申请/颁发服务端证书和客户端证书
默认情况下,用makecert制作的证书,我经过多次尝试,在IE7里始终被认为不信任的证书(也许是我makecert的参数不 对),导致在IE7里测试SSL时,总是显示"证书错误,导航已阻止"之类,所以在本例中,我们换一种方式,用windows2003自带的证书服务来申 请/颁发服务端证书和客户端证书,对这一块不熟悉的朋友,请参见"[原创图解]Win2003证书服务配置/客户端(服务端)证书申请/IIS站点SSL 设置"一文(地址:http://www.cnblogs.com/yjmyzz/archive/2008/08/21/1273201.html), 这里要注意的是服务端证书的"颁发给"的对象一定要和最后运行的url里的计算机名(或域名)信息一致,如下图,否则IE7会认为该证书有问题

二.Wcf web服务端开发
1.vs2008启动后,新建一个web Application(本例命名为WebServer),添加一个wcf服务,命名为MyService,同样系统会自动增加一个IMyService的接口,这二个文件的内容如下:
MyService.svc.cs(代码很简单,返回服务端时间而已)

using System;
using System.ServiceModel;

namespace WebServer
{
    // NOTE: If you change the class name "MyService" here, you must also update the reference to "MyService" in Web.config.
    public class MyService : IMyService
    {
        public string Test()
        {           
            return "服务端时间:<br/>" + DateTime.Now.ToString();
        }
    }
}

IMyService.cs内容

using System;
using System.ServiceModel;
using System.Text;

namespace WebServer
{
    // NOTE: If you change the interface name "IMyService" here, you must also update the reference to "IMyService" in Web.config.
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string Test();
    }
}

配置文件Web.Config关键节点内容如下:

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WebServer.MyServiceBehavior">
                    
                    <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" httpsGetUrl="https://jimmycntvs/MyService.svc"/>
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    <serviceCredentials>
                        <!--下面这一行,在测试过程中,发现始终不起作用,只能放弃,转而在客户端的配置中用findValue="ec0aa48043eab64714c92a0ff7fa0365e1b594af" x509FindType="FindByThumbprint" 类似这样的方法来验证指定的客户端证书-->
                        <!--<clientCertificate>
                            <authentication certificateValidationMode="Custom" customCertificateValidatorType="WebServer.CustomX509CertificateValidator,WebServer"/>
                        </clientCertificate>-->
                        <serviceCertificate findValue="JimmyCntvs" storeLocation="LocalMachine"  x509FindType="FindBySubjectName" storeName="My"/>
                    </serviceCredentials>
                    
                </behavior>                
            </serviceBehaviors>
        </behaviors>
        <bindings>            
            <wsHttpBinding>
                <binding name="wsHttpBinding_MyService">
                    <security mode="Transport">
                        <transport clientCredentialType="None"/>
                        <!--设置成Certificate后,启动WCF时,总是提示出错[ 服务“SslRequireCert”的 SSL 设置与 IIS“Ssl”的 SSL 设置不匹配。]无奈只能设置成None-->
                        <!--<transport clientCredentialType="Certificate"/>-->
                        <message clientCredentialType="Certificate"/>                                                          
                    </security>                   
                </binding>
            </wsHttpBinding>
        </bindings>
        <services>
           <service behaviorConfiguration="WebServer.MyServiceBehavior"
            name="WebServer.MyService">
               <endpoint address="https://jimmycntvs/MyService.svc" binding="wsHttpBinding"
                bindingConfiguration="wsHttpBinding_MyService" contract="WebServer.IMyService" >
                   <identity>
                       <dns value="jimmycntvs"/>
                   </identity>
               </endpoint>           
           </service>
  </services>
    </system.serviceModel>

解释一下:
 <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" httpsGetUrl="https://jimmycntvs/MyService.svc%22/>这行表明,只能用https方式取得wcf的元数据,httpsGetUrl标明了取得WCF元数据的地址
 
 
 <serviceCertificate findValue="JimmyCntvs" storeLocation="LocalMachine"  x509FindType="FindBySubjectName" storeName="My"/>这一句指wcf服务启动时,先验证服务端是否在LocalMachine这个位置有一个SubjectName为 JimmyCntvs的服务端证书
 
 <security mode="Transport">
      <transport clientCredentialType="None"/>                      
      <message clientCredentialType="Certificate"/>                                                         
  </security> 这里表示用Transport模式来进行安全验证,详细安全参数含义可参见http://www.cnblogs.com/yjmyzz/archive/2008/08/19/1271133.html

2.IIS服务端配置
为方便测试,在IIS中直接把WebServer项目配置为一个站点(本例为http://localhost/),同时正确安装第一步颁发的服务端证书,同时把"要求安全通道(SSL)"选中,这样站点就必须用https://来访问了

这些都弄好以后,就可以测试了,浏览https://localhost/MyService.svc,如果是IE7,可能会报一个"证书错误:导航已阻止"的错误,没关系,把localhost换成计算机名(本例中为jimmycntvs)就正常了,如下图:

三.Web 客户端开发
1.先生成服务端WCF的代理
vs2008命令行下运行
svcutil.exe https://jimmycntvs/MyService.svc?wsdl /d:c:"123 即把代理文件和配置文件输出到c:"123下

2.安装客户端证书
把第一步颁发的客户端证书正确安装,同时查看该证书的详细信息,记下"微缩图"去掉空格后的值(本例为ec0aa48043eab64714c92a0ff7fa0365e1b594af,每个证书的这个值都是唯一的),后面会用到

3.vs.net2008新建一个WebClient的webApplication,把刚才的这二个文件加到WebClient中,同时output.config改名为Web.config,关键节点调整如下:

<system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="NewBehavior">
                    <clientCredentials>
                        <!--<clientCertificate findValue="Jimmy.Yang" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My"/>-->
                        <!--由于服务端设置的<authentication certificateValidationMode="Custom" customCertificateValidatorType="WebServer.CustomX509CertificateValidator,WebServer"/>在测试中发现总是不起作用,所以只能转而用下面的方式从客户端来验证特定的证书,理论上讲这样有安全隐患,建议实际操作时,可将本节加密后,再连同客户端证书一起分发给客户端,若用于安全性较高的环境,建议还是用UserName方式,到数据库里验证用户名和密码-->
                        <clientCertificate findValue="ec0aa48043eab64714c92a0ff7fa0365e1b594af" x509FindType="FindByThumbprint" storeLocation="CurrentUser" storeName="My"/>                        <serviceCertificate>
                            <authentication certificateValidationMode="None" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Certificate" negotiateServiceCredential="true"
                            establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://jimmycntvs/MyService.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IMyService" contract="IMyService"
                name="WSHttpBinding_IMyService" behaviorConfiguration="NewBehavior">
                <identity>
                    <dns value="jimmycntvs" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

主要是增加了behaviors这一节,用于查询客户端机器是否有微缩图为ec0aa48043eab64714c92a0ff7fa0365e1b594af的证书
<behaviors>
            <endpointBehaviors>
                <behavior name="NewBehavior">
                    <clientCredentials>                        
                        <clientCertificate findValue="ec0aa48043eab64714c92a0ff7fa0365e1b594af" x509FindType="FindByThumbprint" storeLocation="CurrentUser" storeName="My"/>                        <serviceCertificate>
                            <authentication certificateValidationMode="None" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>     
       
最后,我们在Default.aspx.cs中加几行代码,用来调用WebServer中的WCF

protected void Page_Load(object sender, EventArgs e)
{
    using (MyServiceClient _client = new MyServiceClient()) 
    {
        string _test = _client.Test();
        Response.Write(_test);
    }
}

运行一下,正常的话,应该会返回服务端的时间。 欢迎转载,转载请注明来自cnblogs"菩提树下的杨过"

编后语:
本文演示了如何将WCF Host在IIS中,并对服务端和客户端都采用x.509证书方式来验证,当然这种方式要求每个客户端机器上都必须安装服务端颁发的证书。在互联网环境 下,这可能会给客户端的使用带来麻烦,这时可以采用服务端用x.509方式验证,客户端用经典的用户名/密码的方式来验证,详情可参见http://www.cnblogs.com/fineboy/archive/2008/03/26/1122650.html,写得很详细,一看就知.

转载于:https://www.cnblogs.com/yencain/articles/1389762.html

【转】x.509证书在WCF中的应用(Web/IIS篇)相关推荐

  1. [轉帖]x.509证书在WCF中的应用(Web/IIS篇)

    http://www.cnblogs.com/yjmyzz/archive/2008/08/22/1272836.html 在上一篇"x.509证书在WCF中的应用(CS篇)"里, ...

  2. 【转】x.509证书在WCF中的应用(CS篇)

    [转自]x.509证书在WCF中的应用(CS篇) 为什么要用x.509证书? WCF的服务端和客户端之间,如 果不作任何安全处理(即服务端的<security mode="None&q ...

  3. 深入剖析授权在WCF中的实现[共14篇]

    I.身份(Identity)与安全主体(Security Principal) 从两个重要的概念谈起:Identity与Principal[上篇] 从两个重要的概念谈起:Identity与Princi ...

  4. 数字证书及在WCF中的应用

    一 概念 1.内容 证书的发布机构     证书的有效期     证书所有者(Subject)     签名所使用的算法     指纹以及指纹算法 公钥     私钥 2.存储区 3.有效性 二 作用 ...

  5. DTLS协议中的509证书和密钥如何传输

    DTLS协议 在openssl中,创建DTLS环境都已经被封装好了 ssl_ctx = SSL_CTX_new(DTLS_method()); 接下去我们编程的时候有两种方式去使用DTLS,1 是直接 ...

  6. WCF服务运行找不到X.509证书解决方案

    异常: 无法使用以下搜索标准找到 X.509 证书: StoreName"My".StoreLocation"LocalMachine".FindType&qu ...

  7. WCF---关于无法使用以下搜索标准找到 X.509 证书的问题

    在上篇中的证书验证,我在测试过程中发现这样的问题,"无法使用以下搜索标准找到 X.509 证书的问题" 搜索后解决 1.先将本机证书导出. 运行certmgr.msc. 到处一个. ...

  8. X.509证书的介绍

    目录 1.X.509简介 2.证书组成结构 3.证书文件扩展名 4.生成一个自签名证书的示例 5.生成签名请求及CA 签名 1.X.509简介 X.509是密码学里公钥证书的格式标准.X.509证书已 ...

  9. WCF中安全的那些事!!!

    WCF默认绑定 WCF预先为所有绑定都定义了能满足大多数情形的配置模式,这样的话,只要没有修改某个配置参数,WCF就使用默认的安全模式. 先上一张默认的安全设置表格 绑定 设置 wsHttpBindi ...

最新文章

  1. mysql自动备份脚本,及系统定时备份设置!
  2. php fpm 测试,zabbix4.2 监控PHP-FPM运行状态的数据
  3. lcd取模如何取16位_两种方式实现取16位变量的高低8位, 不严谨对比
  4. Oracle开发常用知识
  5. 学规划或GIS需要安装的软件
  6. 【LeetCode】剑指 Offer 56. 数组中数字出现的次数
  7. 重构-使代码更简洁优美:实际经验之谈(提供一技巧,让你省掉N多代码)
  8. ai的预览模式切换_当AI频繁切换色彩预览模式时 颜色会越变越深 求解?
  9. Codesys提示【CmContainer/Wibukey runtime system is not installed】的解决方法
  10. 服务器系统2008还原,Windows2008系统克隆——GHOST备份还原系统
  11. 修改360抢票的刷新频率+突破8车次限制
  12. 苹果手机测距离_重量仅6g,智能距离检测,安卓苹果平板手机都能用,声光多级提醒...
  13. spring getway的配置
  14. react 调用子(孙)组件方法
  15. java检查html是否闭合,Java Html解析器和闭合标记
  16. Neural Style Transfer: A Review
  17. 《弃子长安》第十五章 人断惊崖
  18. 判定是否互为字符重排(入门算法30)
  19. 多显示器下应用窗口在多个显示器之间切换方法
  20. 一个Android下的自动下载歌词的代码

热门文章

  1. SIM卡应用-OPN,PLMN,SPN
  2. 脑机接口 脑电波分类_脑机接口的商业应用和脑数据的重要性
  3. lammps计算应力
  4. 我居然不知道Vue3可以使用hooks函数实现代码复用?
  5. 各浏览器模拟手机浏览器的方法
  6. wedo设计课程的思路
  7. 免费下载无水印抖音视频
  8. Re:惠普DJ3836打印机之无法连接wifi
  9. 8.5 Python机器学习--微博聚类和音乐分类理论记录
  10. 电脑出现缺少XXX.dll文件的解决方法