【问题标题】:Custom certificate validation in WCF serviceWCF 服务中的自定义证书验证
【发布时间】:2010-12-06 07:19:34
【问题描述】:

我想在我的 WCF 服务中检查客户端证书。

我的目标是只允许具有特定指纹证书的客户端能够与我的服务进行通信。

我的 WCF 服务托管在 IIS 中,我使用的是 basicHttpBinding 和 security mode="transport",凭据类型为“Certificate”。 IIS 需要客户端证书才能与服务通信。

提前感谢您的帮助。

更新: 我的配置:

<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");
        }
    }

这行不通。我可以使用任何有效证书连接到我的服务。

【问题讨论】:

    标签: wcf security iis certificate


    【解决方案1】:

    您可以创建一个派生自 X509CertificateValidator 的类,并使用它对传入证书进行自定义验证。如果由于某种原因想要验证失败,则抛出 SecurityTokenValidationException。

    将 certificateValidationMode 设置为 Custom 并在配置文件的 clientCertificate 服务行为部分中指定您的验证器。

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

    【讨论】:

    • 我尝试在 clientCertificate 中添加自定义验证器,但它不起作用。它似乎旨在验证服务将在双工模式下使用的客户端证书。我需要验证传入的证书(客户端发送的证书)。您是否在这种情况下尝试过此选项?
    • 这可以在客户端或服务端工作,用于验证传入的消息证书。
    • 我已经添加了我的配置。您需要更多信息吗?
    • 刚刚注意到您正在使用传输而不是消息级别的安全性。为此,您需要改用消息级安全性。
    • 您可以使用 TransportWithMessageCredential 作为安全模式。这结合了 HTTPS 和消息安全性。
    【解决方案2】:

    我不认为有“自定义证书验证”和“传输安全”。它仅适用于“消息安全”。

    【讨论】:

    • 不正确。我在传输模式下运行自定义证书验证
    【解决方案3】:

    是的,您可以使用安全设置为 Transport 的 basicHttpBinding,并且您需要挂钩到 IIS 中的 ServiceHost 创建 - 请参阅 Custom Service Host。 如果您创建自定义配置部分来定义验证标准列表,您应该能够验证指纹值、证书或任何其他数据。

    上述所有实现的示例代码可从this CodeProject artticle 下载的代码中获得。

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 2015-05-27
      • 2011-09-28
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多