ServiceStack JsonServiceClient可以向https w /自签名证书发送get请求吗?

ServiceStack JsonServiceClient可以向https w /自签名证书发送get请求吗?

问题描述:

我打电话来使用JsonServiceClient来序列化我的请求对象。我的服务器正在使用https,我创建了一个自签名证书。

I making a call to get using JsonServiceClient to serialize my request object. My server is using https and I created a self signed certificate.

当客户端尝试连接并且服务器响应证书时,会出现异常不信任且服务器的身份尚未经过验证。

It would appear that an exception is thrown when the client tries to connect and the server responds that the certificate is not trusted and that the identity of the server has not been verified.

在浏览器中,我可以忽略此消息。如何让JsonService客户端使用https和自签名证书?

In a browser I can ignore this message. How can I get the JsonService client to work with https and a self signed certificate?

我认为这是一个类似的问题。您可以获得有关ServerCertificateValidationCallback的更多信息这里这里。下面的测试应该提供一个示例/模板来解决JsonServiceClient的不可信问题。显然,编写自己的证书验证存在一些风险。

I think this is a similar issue to what is happening. You can get more information on ServerCertificateValidationCallback here and here. Below is a test that should provide an example/template of getting past the 'not trusted' issue with JsonServiceClient. Obviously, there is some risk in writing your own certificate validation.

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

希望这有帮助。