配置 HttpClientFactory 时出现证书错误 [英] Certificate error when configuring HttpClientFactory

查看:31
本文介绍了配置 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 时出现证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆