在DotNet Core服务堆栈上绕过SSL证书验证 [英] Bypassing SSL Certificate Validation on DotNet Core Service Stack

查看:457
本文介绍了在DotNet Core服务堆栈上绕过SSL证书验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 ServicePointManager.ServerCertificateValidationCallback 在.Net Core中不再存在,而是被替换为:

I know that ServicePointManager.ServerCertificateValidationCallback no longer exists in .Net Core and is instead replaced with:

using(var handler = new System.Net.Http.HttpClientHandler())
{
    using (var httpClient = new System.Net.Http.HttpClient(handler))
    {
        handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
        {
            return true;
        };

    }
}

但是我们当前正在使用 ServiceStack.Core 库,据我所知,它不会公开这样的属性或处理程序本身。

However we are currently using the ServiceStack.Core library which, as far as I can see, does not expose either a property like this or the handler itself.

我如何告诉ServiceStack客户端跳过此代码中的ssl验证?

How would I tell a ServiceStack client to bypass ssl validation in this code?

using(var client = new JsonServiceClient("https://www.google.com"))
{
    var response = client.Get("/results");
}

如果有办法,在Windows和Linux上是否都可以使用?

If there is a way, would this work the same on both Windows and Linux?

推荐答案

JsonServiceClient 是基于.NET HttpWebRequest 已在.NET Core中重写为通过HttpClient包装,因此我们通常建议使用.NET Core以避免这种开销(比.NET 4.5慢得多),并改用<在ServiceStack.HttpClient 中使用href = http://docs.servicestack.net/csharp-client rel = noreferrer> JsonHttpClient,因为它使用 HttpClient 直接在哪里可以在以下位置注入自己的 HttpClientHandler

JsonServiceClient is built on .NET HttpWebRequest which has been rewritten in .NET Core as a wrapper over HttpClient so we'd generally recommend for .NET Core to avoid this overhead (which is much slower than .NET 4.5) and switch to using JsonHttpClient in ServiceStack.HttpClient instead as it uses HttpClient directly and where you can inject your own HttpClientHandler with:

var client = new JsonHttpClient(baseUrl)
{
    HttpMessageHandler = new HttpClientHandler
    {
        UseCookies = true,
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
        ServerCertificateCustomValidationCallback = (req,cert,chain,errors) => true
    }
};

请注意,建议您重用HttpClient实例,因此您应该尝试重用HttpClient实例,并避免处置它们。

Note it's recommended to reuse HttpClient instances so you should try to reuse HttpClient instances when possible and avoid disposing them.

这篇关于在DotNet Core服务堆栈上绕过SSL证书验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆