【问题标题】:Failed to Authenticate HTTPS connection when attempting GET from WebAPI尝试从 WebAPI 获取时无法验证 HTTPS 连接
【发布时间】:2017-05-31 21:02:13
【问题描述】:

我正在使用 ASP.NET Core。我有两个项目:

  1. ASP.NET Core MVC 应用程序
  2. ASP.NET Core Web API 应用程序

如果我尝试使用 Postman 访问 Web API 端点之一,我没有任何问题; /api/values 端点按预期返回。 (这是标准测试端点。)

但是,如果我尝试使用 MVC 应用程序执行相同的操作,则会收到一个非常令人沮丧的错误:

HttpsConnectionFilter[1]
Failed to authenticate HTTPS connection

我正在使用 Kestrel for ASP.NET Core 进行托管。

我有一个使用 PowerShell 创建的自签名 PFX 证书,这是引发异常的代码:

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(new X509Certificate2("localcert.pfx", "xxx"));
var client = new HttpClient(handler);

var content = await client.GetStringAsync("https://localhost:44301/api/values");

如果我要运行这个,我会得到同样的错误:

var client = new HttpClient();
var content = await client.GetStringAsync("https://localhost:44301/api/values");

我的 Kestrel 设置在我的 Program.cs 中是这样的:

var cert = new X509Certificate2("localcert.pfx", "xxx");

var host = new WebHostBuilder()
  .UseKestrel(cfg => cfg.UseHttps(cert))
  .UseUrls("https://localhost:44300")
  .UseContentRoot(Directory.GetCurrentDirectory())
  .UseIISIntegration()
  .UseStartup<Startup>()
  .Build();

host.Run();

我知道我再次为上面的HttpClient 定义了证书,但我很绝望。

谁能提供一些关于为什么会发生这种情况以及我可以如何修复它甚至调试它的见解?我目前正在逐步检查KestrelHttpServer 代码,看看是否能提供一些见解。

这是我从 Kestrel 控制台窗口得到的完整错误:

信息:HttpsConnectionFilter1 无法验证 HTTPS 连接。 System.IO.IOException:身份验证失败,因为远程方已关闭 传输流。在 System.Net.Security.SslState.StartReadFrame(字节 [] 缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest 异步请求) --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult 惰性结果)在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult 结果)在 System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult) 在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean 需要同步) --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionFilter.d__6.MoveNext()

【问题讨论】:

    标签: c# https asp.net-core asp.net-core-mvc kestrel-http-server


    【解决方案1】:

    我为这个问题找到的最直接的解决方案是删除证书并使用信任标志添加它。

    dotnet dev-certs https --clean
    dotnet dev-certs https --trust
    

    PS。我知道这已经过时了,但我只想把它留在这里,让可能会遇到这个问题的人。

    【讨论】:

    • 正确。最简单的解决方案。
    • 直截了当:)
    【解决方案2】:

    我遇到了同样的问题。经过数小时检查所有可能的事情甚至一些不可能的事情后,我设法将其追溯到错误生成的 SSL 证书。

    我是根据本手册创建我的:How to: Create Your Own Test Certificate

    使用以下命令生成证书:

    makecert -sv yourprivatekeyfile.pvk -n "cert name" yourcertfile.cer -r
    

    如果-r 被省略,则会发生描述的错误。

    那么pfx必须按照手册生成。如果只使用cerKestrel 将无法成功启动。

    我通过生成新的 SSL 证书解决了这个问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-15
    • 2020-01-05
    • 1970-01-01
    • 2017-03-14
    • 2014-10-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多