1

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

在 Windows 7 上,此代码有效:

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

但在地铁应用程序上,该字段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 ?

4

1 回答 1

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);
于 2012-05-23T07:30:49.380 回答