zoukankan      html  css  js  c++  java
  • WCF NetTcpBinding Transport安全模式(4) ClientCredentialType证书验证模式基本配置

    WCF NetTcpBinding Transport安全模式(4)    ClientCredentialType证书验证模式----基本配置

    Transport安全模式下,客户端凭据支持三种类型:NoneWindowsCertificate。默认情况下采用Windows凭据类型。前面几个小节示例中一直在使用Windows凭据类型,本小节主要探讨Certificate凭据。

       使用Certificate凭据,首先需要准备服务端和客户端证书。创建证书命令如图11-10所示。

    图11-10            创建证书

    执行图11-10命令,分别创建名为“XuanhunServer”和“XuanhunClient两个证书,用于服务端和客户端,存储区为“CurrentUser”。

    有了证书文件之后,需要修改相关配置启用证书验证。在服务端添加如代码清单11-19所示behaviors配置节,在behaviors配置节中配置服务端证书

    代码清单11-19  配置服务器端证书

     <behaviors>

          <serviceBehaviors>

            <behavior name="validateBehavior">

              <serviceCredentials>

                <serviceCertificate 

                   x509FindType="FindBySubjectName"

                    storeLocation="CurrentUser"

                    storeName="My"

                    findValue=" XuanhunServer"

                />

                <clientCertificate >

                  <authentication certificateValidationMode="None" trustedStoreLocation="CurrentUser" />

                  <certificate />

                </clientCertificate>

              </serviceCredentials>

            </behavior>

          </serviceBehaviors>

        </behaviors>

    在以上代码中,serviceCredentials节用来添加服务端证书配置,该节包含多个serviceCertificate节,用来指定具体服务端证书实例。serviceCredentials配置节可配置凭据信息参看11.1.3节。

    serviceCertificate基本语法如代码清单11-20所示。

    代码清单11-20    serviceCertificate基本语法

    <serviceCertificate findValue="String"

        storeLocation="LocalMachine/CurrentUser"

        storeName="AddressBook/AuthRoot/CertificateAuthority/Disallowed/My/Root/TrustedPeople/TrustedPublisher"

    X509FindType="FindByThumbprint/FindBySubjectName/FindBySubjectDistinguishedName/FindByIssuerName/FindByIssuerDistinguishedName/FindBySerialNumber/FindByTimeValid/FindByTimeNotYetValid/FindByTemplateName/FindByApplicationPolicy/FindByCertificatePolicy/FindByExtension/FindByKeyUsage/FindBySubjectKeyIdentifier"

    />

    serviceCertificate包含属性及描述如表11-7所示。

    11-7   serviceCertificate属性

    属性

    描述

    findValue

    一个字符串,包含要在X.509证书存储中搜索值。此属性中包含类型必须满足指定 X509FindType 要求。默认值为一个空字符串

    storeLocation

    指定客户端可用于验证服务器证书 X.509 证书存储位置。包括以下有效值:

    • LocalMachine:分配给本地计算机证书存储(默认值)
    • CurrentUser:分配给当前用户证书存储

    storeName

    指定要打开X.509证书存储名称。包括以下有效值:

    • AddressBook:其他用户证书存储
    • AuthRoot:第三方证书颁发机构 (CA) 证书存储
    • CertificatAuthority:中间证书颁发机构 (CA) 证书存储
    • Disallowed:吊销证书证书存储
    • My:个人证书证书存储(默认值)
    • Root:受信任根证书颁发机构 (CA) 证书存储
    • TrustedPeople:直接受信任人和资源证书存储
    • TrustedPublisher:直接受信任发行者证书存储

    X509FindType

    定义要执行X.509搜索类型。包括以下有效值:

    • FindByThumbprint
    • FindBySubjectName
    • FindBySubjectDistinguishedName
    • FindByIssuerName
    • FindByIssuerDistinguishedName
    • FindBySerialNumber
    • FindByTimeValid
    • FindByTimeNotYetValid
    • FindByTemplateName
    • FindByApplicationPolicy
    • FindByCertificatePolicy
    • FindByExtension
    • FindByKeyUsage
    • FindBySubjectKeyIdentifier

    serviceCredentialsclientCertificate配置,可以定义一个用于在双工通信模式中对从服务发送到客户端消息进行签名和加密X.509证书。如果服务必须事先拥有客户端证书才能与该客户端进行安全通信,则需要使用此元素。使用双工通信模式时,会出现这种情况。在更为典型请求/响应模式中,客户端会将其证书包含在请求中,服务将使用该证书对发送回客户端响应进行加密和签名。但是,在双工通信模式中,服务没有来自客户端请求,因此服务需要事先具有客户端证书,以确保发送到客户端消息安全。因此,必须通过带外协商来获取客户端证书,并使用此元素指定该证书。在此元素中设置证书用于仅针对配置有MutualCertificateDuplex消息安全身份验证模式绑定加密发送到客户端消息。

    serviceCredentialsclientCertificate配置节基本语法如下:

    <clientCertificate>

     <certificate/>

     <authentication/>

    </clientCertificate>

    serviceCredentialsclientCertificate配置节没有属性,只有两个子元素<certificate/><authentication/>

    <authentication>元素用来指定服务使用客户端证书身份验证行为。基本语法如代码清单11-21所示。

    代码清单11-21    <authentication>元素基本语法

    <authentication

    customCertificateValidatorType="namespace.typeName, [,AssemblyName] [,Version=version number] [,Culture=culture] [,PublicKeyToken=token]"

    certificateValidationMode="ChainTrust/None/PeerTrust/PeerOrChainTrust/Custom"

    includeWindowsGroups="Boolean"

    mapClientCertificateToWindowsAccount="Boolean"

    revocationMode="NoCheck/Online/Offline"

    trustedStoreLocation="CurrentUser/LocalMachine"

    />

    详细属性说明如表11-8所示。

    11-8  <authentication>配置节属性说明

    属性

    描述

    customCertificateValidatorType

    可选字符串。用于验证自定义类型类型和程序集。certificateValidationMode设置为Custom时,必须设置此属性

    certificateValidationMode

    可选枚举。指定用来验证凭据其中一种模式。此特性类型为 System.Servicemodel.Security.X509CertificateValidationMode。如果设置为Custom,还必须提供 customCertificateValidator。默认值为ChainTrust

    includeWindowsGroups

    可选布尔值。指定Windows组是否包含在安全上下文中。将此属性设置为true会影响性能,因为这会导致完全组扩展。如果不需要建立用户所属组列表,请将此属性设置为 false

    mapClientCertificateToWindowsAcccount

    布尔值。指定是否可以使用证书将客户端映射到 Windows标识。为此必须启用Active Directory

    revocationMode

    可选枚举。用于检查吊销证书列表 (RCL) 一种模式。默认值为Online

    trustedStoreLocation

    可选枚举。两个系统存储位置之一: LocalMachineCurrentUser。在向客户端协商服务证书时使用此值。将根据指定存储位置中 受信任人存储执行验证。默认值为CurrentUser

    说明  certificateValidationMode共有以下五种模式。

    None 未执行任何证书验证。

    PeerTrust:如果证书位于被信任存储区中,则有效。

    ChainTrust:如果证书链在受信任根存储区生成证书颁发机构,则证书有效。

    PeerOrChainTrust:如果证书位于被信任存储区或证书链在受信任根存储区生成证书颁发机构,则证书有效。

    Custom:用户必须插入自定义 X509CertificateValidator 以验证证书。

    clientCertificatecertificate基本语法如代码清单11-22所示,属性说明参看表11-7

    代码清单11-22    clientCertificatecertificate基本语法

    <certificate findValue = "String"

    storeLocation = "CurrentUser/LocalMachine"

    storeName="AddressBook/AuthRoot/CertificateAuthority/Disallowed/My/Root/TrustedPeople/TrustedPublisher"

    X509FindType="FindByThumbPrint/FindBySubjectName/FindBySubjectDistinguishedName/FindByIssuerName/FindByIssuerDistinguishedName/FindBySerialNumber/FindByTimeValid/FindByTimeNotYetValid/FindByTemplateName/FindByApplicationPolicy/FindByCertificatePolicy/FindByExtension/FindByKeyUsage/FindBySubjectKeyIdentifier"

    />

    综合上面讲解,回顾代码清单11-19讲解是不是很清晰了呢?客户端配置不用做过多解释,如代码清单11-23所示。

    代码清单11-23    客户端证书配置

        <behaviors >

          <endpointBehaviors>

            <behavior  name="ForListen">

              <clientVia viaUri="net.tcp://127.0.0.1:64590/HelloService"/>

              

              <clientCredentials>

                <clientCertificate

                   findValue="XuanhunClient"

                    storeLocation="CurrentUser"

                    x509FindType="FindBySubjectName"

                    storeName="My"/>

                <serviceCertificate>

                  <authentication certificateValidationMode="None" />

                </serviceCertificate>

              </clientCredentials>

            </behavior>

            

          </endpointBehaviors>

          

        </behaviors>

     ----------------------------------注:本文部分内容改编自《.NET 安全揭秘》


    作者:玄魂
    出处:http://www.cnblogs.com/xuanhun/
    原文链接:http://www.cnblogs.com/xuanhun/ 更多内容,请访问我的个人站点 对编程,安全感兴趣的,加qq群:hacking-1群:303242737,hacking-2群:147098303,nw.js,electron交流群 313717550。
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    关注我:关注玄魂的微信公众号

  • 相关阅读:
    Wooden Sticks(hdu1051)
    Leftmost Digit(hdu1060)(数学题)
    Sum of Remainders(数学题)
    Brain Network (medium)(DFS)
    Brain Network (easy)(并查集水题)
    Collective Mindsets (medium) (逻辑题)
    Collective Mindsets (easy)(逻辑题)
    RMQ with Shifts(线段树)
    Throwing Dice(概率dp)
    圆桌会议
  • 原文地址:https://www.cnblogs.com/xuanhun/p/2560071.html
Copyright © 2011-2022 走看看