2009-10-13 64 views
12

我想檢查我的WCF服務中的客戶端證書。WCF服務中的自定義證書驗證

我的目標是隻允許具有特定指紋的證書的客戶能夠與我的服務進行通信。

我的WCF服務託管在IIS中,我使用basicHttpBinding和安全模式=「傳輸」憑證類型「證書」。 IIS需要客戶端證書來與服務進行通信。

在此先感謝您的幫助。

UPDATE: 我的配置:

<basicHttpBinding> 
<binding 
      name="testBinding" 
     maxReceivedMessageSize="2147483647"> 
     <readerQuotas 
        maxDepth="2147483647" 
       maxStringContentLength="2147483647" 
       maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" 
       maxNameTableCharCount="2147483647" /> 
<security mode="Transport"> 
       <transport clientCredentialType="Certificate"/> 
      </security> 

</binding> 
</basicHttpBinding> 

行爲:

<serviceBehaviors> 
    <behavior name="SomeServiceBehavior"> 
     <serviceMetadata httpsGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <serviceCredentials> 
     <clientCertificate> 
      <authentication certificateValidationMode="Custom" customCertificateValidatorType="SomeService.CustomCertificateValidator,SomeService" /> 
     </clientCertificate> 
     </serviceCredentials>   
    </behavior> 
    </serviceBehaviors> 

服務配置:

<service 
       behaviorConfiguration="SomeServiceBehavior" 
       name="SomeService"> 
     <endpoint 
        address="" 
        binding="basicHttpBinding" 
        bindingConfiguration="testBinding" 
        contract="ISomeService"> 
     </endpoint> 
     </service> 

而對於測試的目的,我實現這種方式驗證:

public class CustomCertificateValidator : X509CertificateValidator 
    { 
     public override void Validate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) 
     {     
      throw new SecurityTokenValidationException("TEST Certificate was not issued by a trusted issuer TEST"); 
     } 
    } 

而這是行不通的。我可以使用任何有效的證書連接到我的服務。

回答

14

您可以創建一個從X509CertificateValidator派生的類,並使用它來對傳入證書進行自定義驗證。如果由於某種原因想要驗證失敗,則拋出SecurityTokenValidationException。

將certificateValidationMode設置爲Custom,並在配置文件的clientCertificate服務行爲部分指定您的驗證器。

How to: Create a Service that Employs a Custom Certificate Validator

+2

我試圖在clientCertificate中添加自定義驗證器,但它不起作用。它似乎設計用於驗證服務將在雙工模式下使用的客戶端證書。我需要驗證傳入證書(客戶端發送的證書)。你在這種情況下嘗試過這個選項嗎? – empi 2009-10-13 12:54:22

+1

這可以在客戶端或服務端工作,並用於驗證傳入消息證書。 – Maurice 2009-10-13 12:57:12

+0

確保將其添加到中,以驗證服務中的客戶端是否是您想要的。 僅用於客戶端來驗證服務。 – Maurice 2009-10-13 13:00:30

7

我不認爲有反正有「自定義證書驗證」與「交通運輸安全」。它只適用於'消息安全'。

+0

不正確。我有傳輸模式下運行的自定義證書驗證 – Jeremy 2015-04-24 20:21:40

1

是的,您可以使用basicHttpBinding並將安全設置爲Transport,並且您需要掛鉤到IIS中的ServiceHost創建 - 請參閱Custom Service Host。 如果您創建自定義配置部分來定義驗證條件列表,您應該能夠驗證指紋值,證書或任何其他數據。

用於實現上述所有操作的示例代碼可從this CodeProject artticle的代碼下載中獲得。