配置HttpClientFactory时出现证书错误

编程入门 行业动态 更新时间:2024-10-14 02:21:35
本文介绍了配置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时出现证书错误

本文发布于:2023-11-14 20:08:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   错误   HttpClientFactory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!