Search code examples
c#docker.net-corerhel7pfx

.net core docker how to access pfx certificate in code with httpclient? trying to access an external api from container which needs cert


  1. This is how i use it my local machine. I use this pfx cert to fetch data from external 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. But this piece of code throws an error as below when running inside 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. How to fix this issue? Should i change the code ?

Solution

  • When used inside a rhel image I needed to set

    ENV WEBSITE_PRIVATE_CERTS_PATH="pathtopfxcert" 
    

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

    COPY localpfxpath.px pathtopfxcert