【问题标题】:How to set CertificateValidationMode on metro style app?如何在 Metro 风格应用上设置 CertificateValidationMode?
【发布时间】:2012-05-03 20:44:23
【问题描述】:

我在 Azure 上有 WCF 自托管服务。我正在尝试制作桌面客户端和 Metro 风格的 App 客户端。我正在使用带有传输安全性和自签名证书的 nettcpbinding。

在 Windows 7 上,此代码有效:

client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
client.GetUpdate(...);

但在 Metro 应用程序上,ServiceCertificate 字段不存在,所以我得到了(预期的)异常

The X.509 certificate CN=SPDEV-1-PC 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 processed, but terminated in a root certificate 
which is not trusted by the trust provider.

如何更改 certificateValidationMode ?

【问题讨论】:

    标签: c# wcf certificate microsoft-metro nettcpbinding


    【解决方案1】:

    我遇到了类似的问题,我通过反思解决了它。试试看。像这样:

    Type credType = typeof (ClientCredential); //enter here type of your credentials
    PropertyInfo credPropInfo1 = credType.GetTypeInfo().GetDeclaredProperty("ServiceCertificate");
    PropertyInfo credPropInfo2 = credPropInfo1.GetType().GetTypeInfo().GetDeclaredProperty("Authentication");
    PropertyInfo credPropInfo3 = credPropInfo2.GetType().GetTypeInfo().GetDeclaredProperty("CertificateValidationMode");
    credPropInfo3.SetValue(yourObject, 0); // use the int value of the enum, suggested 0 for None
    

    更新: 这里有一些愚蠢的代码,对我来说运行良好;)

    var test6 = client.ClientCredentials.GetType().GetTypeInfo().GetDeclaredProperty("ServiceCertificate").GetValue(client.ClientCredentials);
    var test7 = test6.GetType().GetTypeInfo().GetDeclaredProperty("Authentication").GetValue(test6);
    test7.GetType().GetTypeInfo().GetDeclaredField("certificateValidationMode").SetValue(test7, 0);
    test6.GetType().GetTypeInfo().GetDeclaredField("authentication").SetValue(test6, test7);
    client.ClientCredentials.GetType().GetTypeInfo().GetDeclaredField("serviceCertificate").SetValue(client, test6);
    

    【讨论】:

    • 我不能给你正确的代码,因为我不能复制它。在通过即时窗口运行程序时尝试访问该属性。有更多的命名空间可用。如果您可以在运行时通过即时窗口访问它,也可以借助反射来修改属性。
    • 我更新了上面的代码。这对我有用。另一种选择是等待 1-2 周,看看 Release Preview 会带来什么。
    • 从 'var' 获取 test8 上的编译错误,尝试将其设为 X509CertificateValidationMode 但该类不存在...
    • 获取 InvalidOperationException “API 'System.ServiceModel.Description.ClientCredentials.get_ServiceCertificate()' 无法在当前平台上使用。有关详细信息,请参阅 go.microsoft.com/fwlink/?LinkId=248273。” ...不幸的是,链接不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多