简体   繁体   English

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

[英]How to set CertificateValidationMode on metro style app?

I have a WCF self hosted service on Azure. 我在Azure上有WCF自助服务。 I am trying to make a desktop client and a Metro-style App client. 我正在尝试制作一个桌面客户端和一个Metro风格的App客户端。 I am using nettcpbinding with transport security and a self signed certificate. 我将nettcpbinding与传输安全性和自签名证书一起使用。

On windows 7 this code works : 在Windows 7上,此代码有效:

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

but on the metro app the field ServiceCertificate doesn't exist, so I'm getting the (expected) exception 但在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.

how do I change the certificateValidationMode ? 如何更改certificateValidationMode?

I faced a similar problem, I solved it with reflection. 我遇到了类似的问题,我通过反思解决了。 Try sth. 尝试某事。 like this: 像这样:

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

Update: Here some stupid code, that runs fine for me ;) 更新:这里有一些愚蠢的代码,对我来说很好;)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM