ClientCredentialType證書驗證模式----基本配置
在Transport安全模式下,客戶端憑據支持三種類型:None、Windows、 Certificate。默認情況下采用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/FindBySubjectDist
inguishedName/FindByIssuerName/FindByIssuerDistinguishedName/FindBySeri
alNumber/FindByTimeValid/FindByTimeNotYetValid/FindByTemplateName/FindB
yApplicationPolicy/FindByCertificatePolicy/FindByExtension/FindByKeyUsa
ge/FindBySubjectKeyIdentifier"
/>
serviceCertificate包含的屬性及描述如表11-7所示。
表11-7 serviceCertificate屬性


serviceCredentials下的clientCertificate配置,可以定義一個用於在雙工 通信模式中對從服務發送到客戶端的消息進行簽名和加密的X.509證書。如果服務 必須事先擁有客戶端的證書才能與該客戶端進行安全通信,則需要使用此元素。 使用雙工通信模式時,會出現這種情況。在更為典型的請求/響應模式中,客戶端 會將其證書包含在請求中,服務將使用該證書對發送回客戶端的響應進行加密和 簽名。但是,在雙工通信模式中,服務沒有來自客戶端的請求,因此服務需要事 先具有客戶端的證書,以確保發送到客戶端消息的安全。因此,必須通過帶外協 商來獲取客戶端的證書,並使用此元素指定該證書。在此元素中設置的證書用於 僅針對配置有MutualCertificateDuplex消息安全身份驗證模式的綁定加密發送到 客戶端的消息。
serviceCredentials的clientCertificate配置節基本語法如下:
<clientCertificate>
<certificate/>
<authentication/>
</clientCertificate>
serviceCredentials的clientCertificate配置節沒有屬性,只有兩個子元素 <certificate/>和<authentication/>。
<authentication>元素用來指定服務使用的客戶端證書的身份驗證行為 。基本語法如代碼清單11-21所示。
代碼清單11-21 <authentication>元素基本語法
<authentication
customCertificateValidatorType="namespace.typeName, [,AssemblyName] [,Version=version number] [,Culture=culture] [,PublicKeyToken=token]"
certificateValidationMode="ChainTrust/None/PeerTrust/PeerOrChai nTrust/Custom"
includeWindowsGroups="Boolean"
mapClientCertificateToWindowsAccount="Boolean"
revocationMode="NoCheck/Online/Offline"
trustedStoreLocation="CurrentUser/LocalMachine"
/>
詳細的屬性說明如表11-8所示。
表11-8 <authentication>配置節屬性說明

說明 certificateValidationMode共有以下五種模式。
None: 未執行任何證書驗證。
PeerTrust:如果證書位於被信任的人的存儲區中,則有效。
ChainTrust:如果證書鏈在受信任的根存儲區生成證書頒發機構,則證書有效 。
PeerOrChainTrust:如果證書位於被信任的人的存儲區或證書鏈在受信任的根 存儲區生成證書頒發機構,則證書有效。
Custom:用戶必須插入自定義 X509CertificateValidator 以驗證證書。
clientCertificate的certificate基本語法如代碼清單11-22所示,屬性說明 參看表11-7。
代碼清單11-22 clientCertificate的certificate基本語法
<certificate findValue = "String"
storeLocation = "CurrentUser/LocalMachine"
storeName="AddressBook/AuthRoot/CertificateAuthority/Disallowed/My
/Root/TrustedPeople/TrustedPublisher"
X509FindType="FindByThumbPrint/FindBySubjectName/FindBySubjectDist
inguishedName/FindByIssuerName/FindByIssuerDistinguishedName/FindBySeri
alNumber/FindByTimeValid/FindByTimeNotYetValid/FindByTemplateName/FindB
yApplicationPolicy/FindByCertificatePolicy/FindByExtension/FindByKeyUsa
ge/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/