配置HttpClientFactory时出现证书错误

配置HttpClientFactory时出现证书错误

本文介绍了配置HttpClientFactory时出现证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 HttpClientFactory 中添加证书。带有 HttpClient 的旧实现看起来是这样:

I need to add certificate in HttpClientFactory. Old implementation with HttpClient look this:

var cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cookieContainer };

var basePath = Directory.GetCurrentDirectory();
var certificatePath = Path.Combine(basePath, certPath);
var fileExists = File.Exists(certificatePath);

if (!fileExists)
    throw new ArgumentException(certificatePath);

var certificate = new X509Certificate2(certificatePath, certPwd);
handler.ClientCertificates.Add(certificate);

using (var client = new HttpClient(handler))
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));
    client.DefaultRequestHeaders.Add("ApiKey", apiKey);

    var body = new { UserName = username, UserPassword = password };
    var jsonBody = JsonConvert.SerializeObject(body);
    var content = new StringContent(jsonBody, Encoding.UTF8, contentType);

    var loginResponse = client.PostAsync(loginEndpoint, content).Result;
 }

但是当我尝试从 ConfigurePrimaryHttpMessageHandler 获取连接时我无法在处理程序中设置 ClientCertificates 。如何解决呢?

But when I try to get connection from ConfigurePrimaryHttpMessageHandler I can not set ClientCertificates in handler. How can resolve this ?

更新

public void SetUpHttpClients(IServiceCollection services)
{
        var loginEndpoint = Path.Combine(baseApi, "api/authentication);
        var fileExists = File.Exists(certificatePath);

        if (!fileExists)
            throw new ArgumentException(certificatePath);

        var certificate = new X509Certificate2(certificatePath, certPwd);

        services.AddHttpClient("TestClient", client =>
        {
        client.BaseAddress = new Uri(baseApi);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));
        client.DefaultRequestHeaders.Add("ApiKey", apiKey);

        var body = new { Username = username, Password = password };
        var jsonBody = JsonConvert.SerializeObject(body);
        var content = new StringContent(jsonBody, Encoding.UTF8, contentType);

        var loginResponse = client.PostAsync(loginEndpoint, content).Result;

        }).ConfigurePrimaryHttpMessageHandler(() =>
        {
            var cookieContainer = new CookieContainer();
            var handler = new HttpClientHandler
            {
                CookieContainer = cookieContainer
            };
            handler.ClientCertificates.Add(certificate);
            return handler;
});


推荐答案

异常消息是准确的。

调用 ClientCertificates.Add(certificate); 与之前的操作没什么不同

Call ClientCertificates.Add(certificate); no different to how it was done before

services.AddHttpClient("TestClient", client => {
    client.BaseAddress = new Uri(baseApi);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));
    client.DefaultRequestHeaders.Add("ApiKey", apiKey);
})
.ConfigurePrimaryHttpMessageHandler(() => {
    var handler = new HttpClientHandler {
        CookieContainer = cookieContainer
    };
    handler.ClientCertificates.Add(certificate);
    return handler;
});

这篇关于配置HttpClientFactory时出现证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:39