Search code examples
c#sslasp.net-corehttpclient

Ignore SSL connection errors via IHttpClientFactory


I have a problem with a connect from my asp.net core 2.2 project to an https site like there. I use IHttpClientFactory to create typed HttpClient in Startup.cs

services.AddHttpClient<ICustomService, MyCustomService>();

And I don't understand, how can I ignore SSL connection problems without creating HttpClient manually like this

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

Solution

  • Use ConfigureHttpMessageHandlerBuilder:

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