我打电话来使用JsonServiceClient来序列化我的请求对象。我的服务器正在使用https,我创建了一个自签名证书。
当客户端尝试连接并且服务器响应证书不可信并且服务器的身份尚未经过验证时,似乎会抛出异常。
在浏览器中,我可以忽略此消息。如何让JsonService客户端使用https和自签名证书?
答案 0 :(得分:3)
我认为this is a similar issue正在发生的事情。您可以获得有关ServerCertificateValidationCallback here和here的更多信息。下面的测试应该提供一个示例/模板来解决JsonServiceClient的“不可信”问题。显然,编写自己的证书验证存在一些风险。
public void Test()
{
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
var client = new JsonServiceClient();
var response = client.Post<string>("https://localhost/Secure/Route", new MySecureRequest());
Assert.IsNotNull(response);
}
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)
{
//Do something to check the certificate is valid.
return false || cert.Subject.ToUpper().Contains("Something in Cert");
}
希望这有帮助。