简体   繁体   中英

How to set CertificateValidationMode on metro style app?

I have a WCF self hosted service on Azure. I am trying to make a desktop client and a Metro-style App client. I am using nettcpbinding with transport security and a self signed certificate.

On windows 7 this code works :

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

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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