【问题标题】:WCF Certificate Chain, verify programmaticallyWCF 证书链,以编程方式验证
【发布时间】:2011-08-25 19:49:57
【问题描述】:

我正在尝试以编程方式使用证书,而不是使用商店。我正在使用文件名和密码创建X509Certificate2

当我手动将根证书添加到受信任的人的证书存储中时,这可以正常工作。但是,我宁愿不必在每次部署时都这样做——我也宁愿以编程方式处理它。

当我从证书存储中删除根证书时,我得到了一个例外。

我读到的所有内容似乎都在说我必须手动将根证书添加到证书存储中,否则信任链将无法工作。

问题:有没有一种程序化的方式来设置信任链,所以我不必手动进行?

代码如下:

        var serverCert = new X509Certificate2("FullPathToMyCertificate.cer", "Password");
        Client.ClientCredentials.ServiceCertificate.DefaultCertificate = serverCert;

当我尝试使用客户端时发生的异常是:

System.IdentityModel.Tokens.SecurityTokenValidationException 

The X.509 certificate CN=notrealcertname, OU=TPA, OU=BMP, OU=Projects, O=Somebody, C=US is not in the trusted people store. 
The X.509 certificate CN=notrealcertname, OU=TPA, OU=BMP, OU=Projects, O=Somebody, C=US chain building failed. 
The certificate that was used has a trust chain that cannot be verified. 
Replace the certificate or change the certificateValidationMode. 
A certificate chain could not be built to a trusted root authority.

【问题讨论】:

    标签: c# certificate x509


    【解决方案1】:

    默认情况下,使用的组件会验证链 - 当无法验证链时,您会收到该异常。 如果您想做所有事情,包括验证代码中的链,那么您需要 implement "custom validation" and integrate that into the WCF Host:

    Client.ServiceCertificate.Authentication.CertificateValidationMode =
                  X509CertificateValidationMode.Custom;
    Client.ServiceCertificate.Authentication.CustomCertificateValidator =
        new MyCertificateValidator();
    

    另一种选择是完全禁用验证(不适用于生产!!!

    Client.ServiceCertificate.Authentication.CertificateValidationMode =
                  X509CertificateValidationMode.None;
    

    编辑 - 评论后:

    要自己验证链,您应该查看 X509ChainX509Store - 要了解如何实施这样的链验证,请查看 Mono's implementationVerify... 基本上您使用Find 方法在X509Certificate2Collection 中搜索父级等等...带有自定义验证的验证条件由您决定(有效签名,未过期...)。

    MSDN 上的一些参考链接:

    【讨论】:

    • 非常感谢您的快速回复,Yahia。那么,假设我想实现一个 CustomCertificateValidator 来模仿当我不使用 CustomCertificateValidator 但手动将根证书添加到受信任的人中的证书存储。我将在 CustomCertificateValidator 中放入什么?大概默认行为会根据 Trusted People 中的根证书评估 serverCert,确定其 OK,并让客户端接收响应。进行此评估的目的是什么?再次感谢史蒂夫。
    • 客户在这里指的是什么(比如在 Client.ServiceCertificate.),请 naespace?非常感谢。
    • 这不是命名空间。此代码假定它位于派生自 System.ServiceModel.ClientBase 的类中,并且此“ClientBase”公开了一个“ClientCredentials”属性,您可以在该属性上执行“.ServiceCertificate”然后“.Authentication”然后“。 CertificateValidationMode”等等。如果需要确切知道,ServiceCertificate 是一个类 X509CertificateRecipientClientCredential
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2015-10-19
    • 2012-02-06
    • 1970-01-01
    • 2015-08-22
    • 2011-03-12
    • 2022-10-20
    • 1970-01-01
    相关资源
    最近更新 更多