如何将 httpclienthandler 显式传递给 httpclientfactory?

How to pass httpclienthandler to httpclientfactory explicitly?

想过用HttpClientFactory,但是调用的时候需要附上证书目前使用的是HttpClient,但是不知道怎么附上证书。
下面是httpClient代码:

HttpClientHandler httpClientHandler = new HttpClientHandler
{
    SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12,
    ClientCertificateOptions = ClientCertificateOption.Manual
};
httpClientHandler.ClientCertificates.Add(CertHelper.GetCertFromStoreByThumbPrint(_Settings.MtlsThumbPrint, StoreName.My, _Settings.IgnoreCertValidChecking));

httpClientHandler.ServerCertificateCustomValidationCallback = OnServerCertificateValidation;

HttpClient _client = new HttpClient(httpClientHandler)
{
    Timeout = TimeSpan.FromMinutes(1),
    BaseAddress = new Uri(_Settings.BaseUrl)
};

那么,如何将上面的httpClient转成HttpClientFactory呢?

如有任何帮助,我们将不胜感激。

假设您的意思是使用 ServiceCollection,您可以在设置客户端时配置处理程序

services.AddHttpClient("MyClient", client => {
    client.Timeout = TimeSpan.FromMinutes(1),
    client.BaseAddress = new Uri(_Settings.BaseUrl)
})
.ConfigurePrimaryHttpMessageHandler(() => {
    var httpClientHandler = new HttpClientHandler
    {
        SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12,
        ClientCertificateOptions = ClientCertificateOption.Manual
    };
    httpClientHandler.ClientCertificates.Add(CertHelper.GetCertFromStoreByThumbPrint(_Settings.MtlsThumbPrint, StoreName.My, _Settings.IgnoreCertValidChecking));

    httpClientHandler.ServerCertificateCustomValidationCallback = OnServerCertificateValidation;

    return httpClientHandler;
});

注入 IHttpClientFactory 并调用客户端时的那种方式。

var _client = httpClientFactory.CreateClient("MyClient");

创建的客户端已经配置了所需的证书。