Как установить CertificateValidationMode в приложении в стиле метро?

У меня есть автономная служба WCF в Azure. Я пытаюсь создать настольный клиент и клиент приложения в стиле Metro. Я использую 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?


person Lescai Ionel    schedule 23.04.2012    source источник


Ответы (1)


Я столкнулся с похожей проблемой, я решил ее с помощью рефлексии. Попробуйте что-л. нравится:

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);
person Jan K.    schedule 23.05.2012
comment
Я не могу дать вам правильный код, потому что я не могу его воспроизвести. Попробуйте получить доступ к свойству, пока вы запускаете свою программу через непосредственное окно. Доступно больше пространств имен. Если вы можете получить к нему доступ через непосредственные окна во время выполнения, также можно изменить свойство с помощью отражения. - person Jan K.; 24.05.2012
comment
Я обновил код выше. Это работает для меня. Другой вариант — подождать 1-2 недели и посмотреть, что принесет Release Preview. - person Jan K.; 24.05.2012
comment
Получив ошибку компиляции в test8 из 'var', попытался сделать его X509CertificateValidationMode, но класс не существует... - person Lescai Ionel; 24.05.2012
comment
получение исключения InvalidOperationException API «System.ServiceModel.Description.ClientCredentials.get_ServiceCertificate()» нельзя использовать на текущей платформе. Дополнительные сведения см. на странице go.microsoft.com/fwlink/?LinkId=248273. ... ссылка не работает к сожалению. - person Lescai Ionel; 26.05.2012