【问题标题】:.net core docker how to access pfx certificate in code with httpclient? trying to access an external api from container which needs cert.net core docker如何使用httpclient在代码中访问pfx证书?尝试从需要证书的容器访问外部 api
【发布时间】:2020-11-13 14:48:59
【问题描述】:
  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 Container 中运行时会抛出如下错误
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. 如何解决这个问题?我应该更改代码吗?

【问题讨论】:

  • 调试代码时(无容器)是否有效?
  • 它适用于我的本地,如步骤 1 中所述。

标签: c# docker .net-core rhel7 pfx


【解决方案1】:

在我需要设置的 rhel 图像中使用时

ENV WEBSITE_PRIVATE_CERTS_PATH="pathtopfxcert" 

在 dockerfile 中并使用复制 .pfx 证书到同一路径

COPY localpfxpath.px pathtopfxcert

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2018-01-21
    • 2016-10-06
    相关资源
    最近更新 更多