谷歌云平台上的NET Core 2.1无法进行身份验证HTTPS连接,由于意外的数据包格式导致TLS握手失败。

3

我正在尝试在Google Cloud Platform上设置我的Web应用并使用https。当浏览器尝试通过https端口连接时,Kestrel报告如下:

Hosting environment: Development
Content root path: /home/cvanem2/abc/HelloWorldAspNetCore
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
dbug: HttpsConnectionAdapter[1]
      Failed to authenticate HTTPS connection.
System.IO.IOException: The handshake failed due to an unexpected packet format.
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsServerAsync>b__51_1(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionAdapter.InnerOnConnectionAsync(ConnectionAdapterContext context)ext)

我在生产代码中遇到了这个问题,但是使用提供的示例代码也可以轻松地复现。如果您已经设置了Google Cloud Platform项目,请按照以下说明进行操作:
  1. 打开GCP终端窗口
  2. sudo apt-get -y install dotnet-sdk-2.1
  3. dotnet new razor -o HelloWorldAspNetCore
  4. cd HelloWorldAspNetCore/
  5. dotnet dev-certs https
  6. dotnet run
  7. 将Web预览设置为端口5001并尝试打开。错误将显示在终端窗口中
完全相同的代码在Windows PC上运行良好。我还将Windows代码编译成Linux Docker容器并在Windows上执行了Docker。如果在Docker中执行代码,则会出现问题,因此似乎与运行.net代码的操作系统有关。我尝试使用Wireshark分析来自Docker镜像的TLS数据包,但在前几个数据包之后,数据就被加密了,并且没有发现任何错误。感谢您的帮助。

我在我的Linux工作站上重现了这个错误,所以我不认为GCP是原因。仍在寻找解决方法或解决方案... - Jeffrey Rennie
@Chris,你能解决这个问题吗?能否请你分享一下解决方案。谢谢。 - ram kris
1个回答

4

您可以更改launchSettings.json文件,修改http为https。

"WebApplication2": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "api/values",
  "environmentVariables": {      
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  **"applicationUrl": "http://localhost:5000;https://localhost:5001"**
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接