.net核心docker如何使用httpclient在代码中访问pfx证书?试图从需要证书的容器访问外部api [英] .net core docker how to access pfx certificate in code with httpclient? trying to access an external api from container which needs cert

查看:64
本文介绍了.net核心docker如何使用httpclient在代码中访问pfx证书?试图从需要证书的容器访问外部api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 这是我在本地计算机上使用它的方式.我使用此pfx证书从外部API提取数据.

services.AddHttpClient("test", c =>{}).ConfigurePrimaryHttpMessageHandler(() => 
            {
                var handler = new HttpClientHandler
                {
                    ClientCertificateOptions = ClientCertificateOption.Manual,
                    SslProtocols = SslProtocols.Tls12
                }
            
               handler.ClientCertificates.Add(newX509Certificate2("pathtopfxcert", "pathtokey"));

               return handler;
            }

  1. 但是在RHEL容器中运行时,这段代码会引发如下错误

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HM47J1331JFT", Request id "0HM47J1331JFT:00000001": An unhandled exception was thrown by the application.
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
   at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)

  1. 如何解决此问题?我应该更改代码吗?

推荐答案

在需要设置的rhel图片中使用

When used inside a rhel image I needed to set

ENV WEBSITE_PRIVATE_CERTS_PATH="pathtopfxcert" 

在dockerfile中,然后使用.px复制.pfx证书到同一路径.

in the dockerfile and copy .pfx cert to that same path using

COPY localpfxpath.px pathtopfxcert

这篇关于.net核心docker如何使用httpclient在代码中访问pfx证书?试图从需要证书的容器访问外部api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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