我正在使用ASP.Net Core,我有2个项目。
如果我使用Postman尝试其中一个WebApi端点,我没有任何问题,并且/ api / values按预期返回(标准测试端点)
但是,如果我使用MVC应用程序尝试相同的操作,我会遇到一个非常令人沮丧的错误:
HttpsConnectionFilter[1]
Failed to authenticate HTTPS connection
我正在使用Kestrel托管Asp.Net核心。
我有一个使用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控制台窗口获取的完整错误
info:HttpsConnectionFilter 1 无法验证HTTPS连接。 System.IO.IOException:身份验证失败,因为远程方已关闭 运输流。在 System.Net.Security.SslState.StartReadFrame(Byte []缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest) ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult的 结果)在 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) ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionFilter.d__6.MoveNext()
答案 0 :(得分:3)
我遇到了同样的问题。经过几个小时的检查,甚至是一些不可能的事情,我设法将其追溯到错误生成的SSL证书。
我是根据本手册创建的:How to: Create Your Own Test Certificate。
使用以下命令生成证书:
makecert -sv yourprivatekeyfile.pvk -n "cert name" yourcertfile.cer -r
如果省略-r
,则会发生描述错误。
然后必须根据手册生成pfx
。如果只使用cer
,Kestrel
将无法成功启动。
我通过生成新的SSL证书解决了这个问题。
答案 1 :(得分:0)
我为此问题找到的最直接的解决方案是删除证书,并添加带有信任标志的证书。
dotnet dev-certs https --clean
dotnet dev-certs https --trust
PS。我知道这已经很老了,但我只想把这个留给可能会遇到这个问题的人。