.NET CORE 2.1。通过HTTPS将问题从一个Docker容器发布到另一个Docker

时间:2018-12-06 22:10:35

标签: c# docker .net-core asp.net-core-webapi

在Linux容器(Windows是主机)上,我有2个docker应用程序,都为.NET Core,它们需要交谈:一个,首先称为它,将数据发布到另一个,再称为第二。两者都使用茶est。 在Docker下,我创建了一个网络(使用docker network命令),因此这两个应用程序可以相互通信。

我正在使用Visual Studio 2017进行调试。 我创建控制台应用程序以开始通信。此应用程序将数据发布到First docker应用程序, 进行一些按摩并将其发布到Second docker应用以获得一些爱。

直到从第一个应用发布到第二个应用为止,一切都进行得很好,但是在发布过程中,我看到了奇怪的错误,无法解密。

这是我为第二个应用程序创建WebHost实例的方式,该实例接收帖子:

        WebHost.CreateDefaultBuilder(args)
        .UseKestrel(kestrelOptions =>
        {
            kestrelOptions.ConfigureHttpsDefaults(m=>
            {
                m.SslProtocols |= System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12;
            });
        })
        .UseUrls("http://*:44348", "http://*:443") // listen on all network interfaces
        .UseIISIntegration()
        .UseStartup<Startup>();

这是First进行发布的方式,其中httpClient是HttpClient类型:

        StringContent content = new StringContent($"{{\"field1\" : \"{obj.Field1}\", \"field2\":\"{obj.Field2}\"}}"
                                                    , Encoding.UTF8
                                                    , "application/json");
        var response = httpClient.PostAsync("Test\\Log", content)?
                                            .GetAwaiter()
                                            .GetResult();

这里是发布的完整网址:https://172.19.0.2:44348/api/Test/Log,其中指定的IP地址是我创建的Docker网络中的节点IP地址。

  

似乎正在尝试通过http发送https请求,   因为如果我从第一个应用程序下面的Dockerfile中删除“ EXPOSE 80”   返回“没有通往主机的路由”消息。

这是Second app的Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 44348

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Second.csproj" "./"]
RUN dotnet restore "/Second.csproj"
COPY . .
WORKDIR /src/
RUN dotnet build "Second.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Second.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Second.dll"]

这是第一个应用程序获得的(发送方):

  

抛出异常:“ System.Net.Http.HttpRequestException”   System.Private.CoreLib.dll System.Net.Http.HttpRequestException:   无法建立SSL连接,请参阅内部异常。 ->   System.IO.IOException:握手由于意外而失败   数据包格式。   在   System.Net.Security.SslState.StartReadFrame(Byte []缓冲区,Int32   readBytes,AsyncProtocolRequest asyncRequest)   System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest   asyncRequest)   ---从先前抛出异常的位置开始的堆栈跟踪---位于System.Net.Security.SslState.ThrowIfExceptional()
  在   System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult   lazyResult)   System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult   结果)   System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult   asyncResult)   System.Net.Security.SslStream。<> c.b__47_1(IAsyncResult   )   System.Threading.Tasks.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func 2 endFunction,操作1 endAction, Task 1承诺,布尔值   需要同步)   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Net.Http.ConnectHelper。EstablishmentSslConnectionAsyncCore(流   流,SslClientAuthenticationOptions sslOptions,CancellationToken   cancelToken)-内部异常堆栈跟踪的结尾-在   System.Net.Http.ConnectHelper。EstablishmentSslConnectionAsyncCore(流   流,SslClientAuthenticationOptions sslOptions,CancellationToken   cancelToken)   System.Threading.Tasks.ValueTask 1.get_Result() at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask 1.get_Result()位于   System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask 1 creationTask) at System.Threading.Tasks.ValueTask 1.get_Result()
  在   System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage   request,布尔值doRequestAuth,CancellationToken cancelledToken)
  在System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage   的请求,在   Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage   的请求,在   Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage   的请求,在   System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask,   HttpRequestMessage请求,CancellationTokenSource cts,布尔值   disposeCts)

这些美女第二个应用程序获取(接收方):

  

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:   无效的请求行:   '\ x16 \ x03 \ x01 \ x019 \ x01 \ x00 \ x015 \ x03 \ x03>〜\ x0C \ xDAl2 \ xD9 \ xDC \ xB5 * \ xD5 \ x95 \ x8D \ xC000 \ x8Aa \ xE6 \ xC3 \ xF7 \ xE9 \ xA1&<\ xF8 \ x02 [\ x90U \ xA3 \ x0E \ x00 \ x00 \ xA2 \ xC00 \ xC0,\ xC0(\ xC0 $ \ xC0 \ x14 \ xC0 \ x0A'   在   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.RejectRequestLine(Byte* requestLine, Int32 length) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.GetUnknownMethod(Byte *   数据,Int32长度,Int32&methodLength)在   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler handler, Byte* data, Int32 length) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler   处理程序ReadOnlySequence 1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser.ParseRequestLine(TRequestHandler   处理程序,ReadOnlySequence 1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeStartLine(ReadOnlySequence 1个缓冲区,SequencePosition&已消耗,SequencePosition&已检查)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence 1 buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication 1   申请)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync [TContext](IHttpApplication 1 application) [20:43:37 INF] Microsoft.AspNetCore.Server.Kestrel: Connection id "0HLIRMJN7758A" bad request data: "Invalid request line: '\x16\x03\x01\x019\x01\x00\x015\x03\x03>~\x0C\xDAl2\xD9\xDC\xB5*\xD5\x95\x8D\xC000\x8Aa\xE6\xC3\xF7\xE9\xA1&<\xF8\x02[\x90U\xA3\x0E\x00\x00\xA2\xC00\xC0,\xC0(\xC0$\xC0\x14\xC0\x0A'" Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Invalid request line: '\x16\x03\x01\x019\x01\x00\x015\x03\x03>~\x0C\xDAl2\xD9\xDC\xB5*\xD5\x95\x8D\xC000\x8Aa\xE6\xC3\xF7\xE9\xA1&<\xF8\x02[\x90U\xA3\x0E\x00\x00\xA2\xC00\xC0,\xC0(\xC0$\xC0\x14\xC0\x0A' at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.RejectRequestLine(Byte * requestLine,Int32 length)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.GetUnknownMethod(Byte* data, Int32 length, Int32& methodLength) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler   处理程序,字节*数据,Int32长度)位于   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler handler, ReadOnlySequence 1&缓冲区,SequencePosition&已消耗,   SequencePosition&已检查)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseRequestLine(TRequestHandler handler, ReadOnlySequence 1&缓冲区,SequencePosition&已消耗,   SequencePosition&已检查)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeStartLine(ReadOnlySequence 1 buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence 1   缓冲区,SequencePosition&已消耗,SequencePosition&已检查)   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result,Boolean&endConnection)位于   Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests [TContext](IHttpApplication 1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication 1   应用)[41m [30mfail [39m [22m [49m:   JDBJwtToken.Middlewares.BadRequestDiagnosticAdapter [0]         观察到400响应。请求数据的最后318个字节是:         [HEX] 16 03 01 01 39 01 00 01 35 03 03 3E 7E 0C DA 6C 32 D9 DC B5 2A D5 95 8D C0 30 30 8A 61 E6 C3 F7 E9 A1 26 3C F8 02 5B 90 55 A3   0E 00 00 A2 C0 30 C0 2C C0 28 C0 24 C0 14 C0 0A 00 A5 00 A3 00 A1 00   9F 00 6B 00 6A 00 69 00 68 00 39 00 38 00 37 00 36 00 88 00 87 00 86   00 85 C0 19 00 A7 00 6D 00 3A 00 89 C0 32 C0 2E C0 2A C0 26 C0 0F C0   05 00 9D 00 3D 00 35 00 84 C0 2F C0 2B C0 27 C0 23 C0 13 C0 09 00 A4   00 A2 00 A0 00 9E 00 67 00 40 00 3F 00 3E 00 33 00 32 00 31 00 30 00   9A 00 99 00 98 00 97 00 45 00 44 00 43 00 42 C0 18 00 A6 00 6C 00 34   00 9B 00 46 C0 31 C0 2D C0 29 C0 25 C0 0E C0 04 00 9C 00 3C 00 2F 00   96 00 41 00 FF 01 00 00 6A 00 00 00 11 00 0F 00 00 0C 39 36 30 37 32   37 33 38 34 65 35 39 00 0B 00 04 03 00 01 02 00 0A 00 1C 00 1A 00 17   00 19 00 1C 00 1B 00 18 00 1A 00 16 00 0E 00 0D 00 0B 00 0C 00 09 00   0A 00 23 00 00 00 0D 00 20 00 1E 06 01 06 02 06 03 05 01 05 02 05 03   04 01 04 02 04 03 03 01 03 02 03 03 02 01 02 02 02 03 00 0F 00 01 01

     

[RAW] 9?5>〜Úl2Ùܵ Õ00aæÃ÷é'&<ø[U£??¢0À,À(ÀÀÀ         ?¥?£?¡?????? k?j?i?h?9?8?7?6 ???? ??????????????????????? ?? g?@ ???>?3?2?1?0 ????? E?D?C?BÀ?l?4 ??FÀ1-)%% ???   [HEX] 16 03 01   01 39 01 00 01 35 03 03 3E 7E 0C DA 6C 32 D9 DC B5 2A D5 95 8D C0 30   30 8A 61 E6 C3 F7 E9 A1 26 3C F8 02 5B 90 55 A3 0E 00 00 A2 C0 30 C0   2C C0 28 C0 24 C0 14 C0 0A 00 A5 00 A3 00 A1 00 9F 00 6B 00 6A 00 69   00 68 00 39 00 38 00 37 00 36 00 88 00 87 00 86 00 85 C0 19 00 A7 00   6D 00 3A 00 89 C0 32 C0 2E C0 2A C0 26 C0 0F C0 05 00 9D 00 3D 00 35   00 84 C0 2F C0 2B C0 27 C0 23 C0 13 C0 09 00 A4 00 A2 00 A0 00 9E 00   67 00 40 00 3F 00 3E 00 33 00 32 00 31 00 30 00 9A 00 99 00 98 00 97   00 45 00 44 00 43 00 42 C0 18 00 A6 00 6C 00 34 00 9B 00 46 C0 31 C0   2D C0 29 C0 25 C0 0E C0 04 00 9C 00 3C 00 2F 00 96 00 41 00 FF 01 00   00 6A 00 00 00 11 00 0F 00 00 0C 39 36 30 37 32 37 33 38 34 65 35 39   00 0B 00 04 03 00 01 02 00 0A 00 1C 00 1A 00 17 00 19 00 1C 00 1B 00   18 00 1A 00 16 00 0E 00 0D 00 0B 00 0C 00 09 00 0A 00 23 00 00 00 0D   00 20 00 1E 06 01 06 02 06 03 05 01 05 02 05 03 04 01 04 02 04 03 03   01 03 02 03 03 03 02 01 02 02 02 03 00 0F 00 01 01
  [RAW] 9   [20:43:37错误] JDBJwtToken.Middlewares.BadRequestDiagnosticAdapter:   观察到400响应。请求数据的最后318个字节是:
  [十六进制]   16 03 01 01 39 01 00 01 35 03 03 3E 7E 0C DA 6C 32 D9 DC B5 2A D5 95   8D C0 30 30 8A 61 E6 C3 F7 E9 A1 26 3C F8 02 5B 90 55 A3 0E 00 00 A2   C0 30 C0 2C C0 28 C0 24 C0 14 C0 0A 00 A5 00 A3 00 A1 00 9F 00 6B 00   6A 00 69 00 68 00 39 00 38 00 37 00 36 00 88 00 87 00 86 00 85 C0 19   00 A7 00 6D 00 3A 00 89 C0 32 C0 2E C0 2A C0 26 C0 0F C0 05 00 9D 00   3D 00 35 00 84 C0 2F C0 2B C0 27 C0 23 C0 13 C0 09 00 A4 00 A2 00 A0   00 9E 00 67 00 40 00 3F 00 3E 00 33 00 32 00 31 00 30 00 9A 00 99 00   98 00 97 00 45 00 44 00 43 00 42 C0 18 00 A6 00 6C 00 34 00 9B 00 46   C0 31 C0 2D C0 29 C0 25 C0 0E C0 04 00 9C 00 3C 00 2F 00 96 00 41 00   FF 01 00 00 6A 00 00 00 11 00 0F 00 00 0C 39 36 30 37 32 37 33 38 34   65 35 39 00 0B 00 04 03 00 01 02 00 0A 00 1C 00 1A 00 17 00 19 00 1C   00 1B 00 18 00 1A 00 16 00 0E 00 0D 00 0B 00 0C 00 09 00 0A 00 23 00   00 00 0D 00 20 00 1E 06 01 06 02 06 03 05 01 05 02 05 03 04 01 04 02   04 03 03 01 03 02 03 03 02 01 02 02 02 03 00 0F 00 01 01
  [生的]   9?5>〜Úl2Ùܵ *Õ00aæÃ÷é¡&<ø[U£??¢0À,À(À   ?¥?£?¡?????? k?j?i?h?9?8?7?6 ????   ????????????????????? ?? g?@ ???>?3?2?1?0 ????? E?D?C?BÀ?l?4 ??FÀ1-)%% ???

如果某位性骚扰者可以帮助我解决该情况,我将非常有帮助。

1 个答案:

答案 0 :(得分:1)

您的应用仅在侦听HTTP协议,请参见此处:

.UseUrls("http://*:44348", "http://*:443") // listen on all network interfaces 

向其发布时,将URL更改为http://172.19.0.2:44348/api/Test/Log,它将起作用。

  

注意:那是不安全的。

要将Kestrel配置为使用证书,您需要使用listenOptions.UseHttps(certificate);传递它,请参见此处以获取更多信息:

https://blogs.msdn.microsoft.com/webdev/2017/11/29/configuring-https-in-asp-net-core-across-different-platforms/