如何在地铁风格的应用程序上设置证书验证模式
本文关键字:设置 程序上 证书 验证 模式 应用程序 应用 地铁 风格 | 更新日期: 2023-09-27 18:31:54
我在Azure上有一个WCF自承载服务。我正在尝试制作桌面客户端和 Metro 风格的应用程序客户端。我正在使用带有传输安全和自签名证书的网络绑定。
在 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.
如何更改证书验证模式?
我遇到了类似的问题,我用反思解决了它。试试sth。喜欢这个:
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);