尝试从 WebAPI 获取时无法验证 HTTPS 连接
Failed to Authenticate HTTPS connection when attempting GET from WebAPI
我正在使用 ASP.NET Core。我有两个项目:
- ASP.NET 核心 MVC 应用程序
- ASP.NET 核心 Web API 应用程序
如果我尝试使用 Postman 访问 Web API 端点之一,我没有任何问题; /api/values
端点 returns 符合预期。 (这是标准测试端点。)
但是,如果我尝试使用 MVC 应用程序执行相同的操作,我会收到一个非常令人沮丧的错误:
HttpsConnectionFilter[1]
Failed to authenticate HTTPS connection
我正在使用 Kestrel 为 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 控制台得到的完整错误 window:
info: HttpsConnectionFilter1
Failed to authenticate HTTPS connection. System.IO.IOException: Authentication failed because the remote party has closed the
transport stream. 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.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 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.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult
iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean
requiresSynchronization)
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionFilter.d__6.MoveNext()
我遇到了同样的问题。经过几个小时检查所有可能的甚至一些不可能的东西后,我设法将其追溯到错误生成的 SSL 证书。
我是根据本手册创建我的:How to: Create Your Own Test Certificate。
证书是使用此命令生成的:
makecert -sv yourprivatekeyfile.pvk -n "cert name" yourcertfile.cer -r
如果 -r
被省略,就会发生描述的错误。
那么pfx
必须按照手册生成。如果只使用cer
,Kestrel
将无法启动。
我通过生成新的 SSL 证书解决了这个问题。
对于这个问题,我找到的最直接的解决方案是删除证书并添加信任标志。
dotnet dev-certs https --clean
dotnet dev-certs https --trust
PS。我知道这已经过时了,但我只想把它留给可能遇到这个问题的人。
我正在使用 ASP.NET Core。我有两个项目:
- ASP.NET 核心 MVC 应用程序
- ASP.NET 核心 Web API 应用程序
如果我尝试使用 Postman 访问 Web API 端点之一,我没有任何问题; /api/values
端点 returns 符合预期。 (这是标准测试端点。)
但是,如果我尝试使用 MVC 应用程序执行相同的操作,我会收到一个非常令人沮丧的错误:
HttpsConnectionFilter[1]
Failed to authenticate HTTPS connection
我正在使用 Kestrel 为 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 控制台得到的完整错误 window:
info: HttpsConnectionFilter1 Failed to authenticate HTTPS connection. System.IO.IOException: Authentication failed because the remote party has closed the transport stream. 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.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 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.Threading.Tasks.TaskFactory
1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionFilter.d__6.MoveNext()
我遇到了同样的问题。经过几个小时检查所有可能的甚至一些不可能的东西后,我设法将其追溯到错误生成的 SSL 证书。
我是根据本手册创建我的:How to: Create Your Own Test Certificate。
证书是使用此命令生成的:
makecert -sv yourprivatekeyfile.pvk -n "cert name" yourcertfile.cer -r
如果 -r
被省略,就会发生描述的错误。
那么pfx
必须按照手册生成。如果只使用cer
,Kestrel
将无法启动。
我通过生成新的 SSL 证书解决了这个问题。
对于这个问题,我找到的最直接的解决方案是删除证书并添加信任标志。
dotnet dev-certs https --clean
dotnet dev-certs https --trust
PS。我知道这已经过时了,但我只想把它留给可能遇到这个问题的人。