2009-10-13 65 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的代码下载中获得。