通过 IHttpClientFactory 忽略 SSL 连接错误

Ale*_*nov 6 c# ssl httpclient asp.net-core

我在从我的 asp.net core 2.2 项目连接到像那里这样的 https 站点时遇到问题。我使用 IHttpClientFactory 在 Startup.cs 中创建类型化的 HttpClient

services.AddHttpClient<ICustomService, MyCustomService>();
Run Code Online (Sandbox Code Playgroud)

我不明白,如何在不像这样手动创建 HttpClient 的情况下忽略 SSL 连接问题

using (var customHandler = new HttpClientHandler())
{
    customHandler.ServerCertificateCustomValidationCallback  = (m, c, ch, e) => { return true; };
    using (var customClient = new HttpClient(customHandler)
    {
        // my code
    }
}
Run Code Online (Sandbox Code Playgroud)

Art*_*tur 12

使用ConfigureHttpMessageHandlerBuilder

services.AddHttpClient<ICustomService, MyCustomService>()
    .ConfigureHttpMessageHandlerBuilder(builder =>
    {
        builder.PrimaryHandler = new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
        };
    });
Run Code Online (Sandbox Code Playgroud)