Docker .net core SSL连接无法建立异常

sur*_*put 6 c# ssl https docker asp.net-core-webapi

我正在尝试从 docker 容器连接 https 端点。我已经使用最新的 .net core web api 3.1 创建了该项目当我从容器中使用 https 端点时,我随机不断地收到异常

Exception: System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer.
 ---> System.Net.Sockets.SocketException (104): Connection reset by peer
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__65_1(IAsyncResult iar)
Run Code Online (Sandbox Code Playgroud)

然而,当我在 Visual Studio 中使用调试模式测试相同的内容时,我得到了来自所有 https 端点的响应,但 docker 中的相同内容会随机中断,但出现上述异常。

这是我的 docker 文件

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src/myapp
COPY . .
RUN dotnet restore -nowarn:msb3202,nu1503
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myapp.dll"]
Run Code Online (Sandbox Code Playgroud)

这是我的 docker run 命令

docker run -d -p 443:443 -e ASPNETCORE_URLS="https://+443;http://+80" -e ASPNETCORE_HTTPS_PORT=443 -e ASPNETCORE_Kestrel__Certificates__Default__Password=****** -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/mycert.pfx -v ${HOME}/.aspnet/https:/https/ --link=myappredis --name myappapi myapp-v1
Run Code Online (Sandbox Code Playgroud)

我已经尝试了这两种方法,即使用 HTTPWebRequest 或使用 HTTPClient,但我在容器化应用程序中遇到了与上述相同的异常。

这是我的 HTTP Wrapper 源代码

public class HTTPResponse
    {
        public static async Task<(string, bool)> GetHTTPResponse(HTTPRequestParams request)
        {
            HttpWebResponse httpResponse = null;
            HttpWebRequest httpWebRequest = null;
            string apiResponse = "";
            try
            {
                httpWebRequest = (HttpWebRequest)WebRequest.Create(request.url);
                httpWebRequest.Method = request.method.ToString();
                httpWebRequest.Timeout = request.serviceTimeout * 1000;
                //httpWebRequest.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                httpResponse = (HttpWebResponse)(await httpWebRequest.GetResponseAsync());

                if (request.GZIP)
                {
                    using (var decompress = new GZipStream(httpResponse.GetResponseStream(), CompressionMode.Decompress))
                    using (var streamReader = new StreamReader(decompress))
                    {
                        apiResponse = await streamReader.ReadToEndAsync();
                    }
                }
                else
                {
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        apiResponse = await streamReader.ReadToEndAsync();
                    }
                }
                return (apiResponse, true);
            }
            catch (WebException ex)
            {
                Console.WriteLine($"URL: {request.url}| Exception: {ex.ToString()}");
                return ("", false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"URL: {request.url}| Exception: {ex.ToString()}");
                return ("", false);
            }
            finally
            {
                if (httpResponse != null)
                {
                    httpResponse.Close();
                }
            }
        }

        public static async Task<(string, bool)> GetHTTPResponse1(HTTPRequestParams request)
        {
            try
            {
                string apiResponse = "";
                var client = new HttpClient();
                var response = await client.GetAsync(request.url);
                response.EnsureSuccessStatusCode();
                apiResponse = await response.Content.ReadAsStringAsync();
                return (apiResponse, true);
            }
            catch (WebException ex)
            {
                Console.WriteLine($"URL: {request.url}| Exception: {ex.ToString()}");
                return ("", false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"URL: {request.url}| Exception: {ex.ToString()}");
                return ("", false);
            }
        }
        public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification,

            System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不知道我在这里错过了什么。我尝试绕过 httpWebRequest.ServerCertificateValidationCallback 但仍然没有锁定。