在DotNet核心服务堆栈上绕过SSL证书验证

时间:2017-06-06 20:25:52

标签: c# linux servicestack .net-core

我知道.Net Core中不再存在# Keycloak keycloak.realm=local keycloak.realmKey=MIIBIjANBgkqhkiG9wsIIBCgKCAQEAuJYmaWvF3YhifflJhspXOs8RJn74w+eVD8PtpVbu2cYG9OIa49P8SwqVn/kyJQr7kT3OlCq3XMZWBHe+JSzSz7KttKkhfFSfzISdKDKlkPena2H/i3FKlRZIldbeeuQNYdD6nMpzU6QWLwGF1cUAo1M11f2p99QI1FOhVPJSErWsjDsKpWqG+rMMjT1eos0QCNP7krx/yfMdlUyaJCYiDvpOAoec3OWXvDJovEajBNAZMWVXgJF90wAVPRF6szraA2m7K2gG9ozaCNWB0v4Sy6czekbKjqEBPJo45uEmGHd92V//uf/WQG4HSiuv8CTV+b6TQxKtZCpQpqp2DyCLewIDAQAB keycloak.auth-server-url=http://localhost:8080/auth keycloak.ssl-required=none keycloak.resource=App-backend keycloak.bearer-only=true keycloak.credentials.secret=a714aede-5af9-4560-8c9d-d655c831772f keycloak.securityConstraints[0].securityCollections[0].name=Secured API keycloak.securityConstraints[0].securityCollections[0].authRoles[0]=ROLE_USER keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/api/* ,而是替换为:

ServicePointManager.ServerCertificateValidationCallback

但是我们目前正在使用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客户端绕过此代码中的ssl验证?

ServiceStack.Core

如果有办法,Windows和Linux上的工作方式是否相同?

1 个答案:

答案 0 :(得分:9)

JsonServiceClient是基于.NET HttpWebRequest构建的,它已经在.NET Core中重写为wrapper over HttpClient所以我们通常建议.NET Core避免这种开销(这是比.NET 4.5慢得多,并转而使用JsonHttpClient in ServiceStack.HttpClient,因为它直接使用HttpClient,您可以在其中注入自己的HttpClientHandler

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

请注意,建议reuse HttpClient instances,因此您应该尝试尽可能重用HttpClient实例并避免丢弃它们。