如何在metro风格的应用程序上设置CertificateValidationMode?

时间:2012-04-23 08:25:45

标签: c# wcf certificate microsoft-metro nettcpbinding

我在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?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我用反思来解决它。试试吧像这样:

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