gpt4 book ai didi

c# - 基础连接已关闭。无法使用 HTTP.sys 运行 Asp.Net Core 2.1 Web API 应用程序?

转载 作者:可可西里 更新时间:2023-11-01 08:49:56 24 4
gpt4 key购买 nike

我创建了一个新的 Asp.net core 2.1 web 应用程序,然后选择“API”模板。 (我将身份验证更改为“Windows”。然后我添加了以下代码以使用 Http.Sys 进行 Windows 身份验证。(https://learn.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-2.1&tabs=aspnetcore2x)

using Microsoft.AspNetCore.Server.HttpSys; // Added

public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
// Added
.UseHttpSys(o =>
{
o.Authentication.Schemes = AuthenticationSchemes.NTLM |
AuthenticationSchemes.Negotiate;
o.Authentication.AllowAnonymous = false;
});

以下消息显示了运行应用程序时的输出消息。

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]      User profile is available. Using 'C:\Users\...\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.info: Microsoft.AspNetCore.Server.HttpSys.HttpSysListener[0]      Startinfo: Microsoft.AspNetCore.Server.HttpSys.HttpSysListener[0]      Listening on prefix: https://localhost:5001/info: Microsoft.AspNetCore.Server.HttpSys.HttpSysListener[0]      Listening on prefix: http://localhost:5000/Hosting environment: DevelopmentContent root path: C:\work\WebApplication1Now listening on: https://localhost:5001Now listening on: http://localhost:5000Application started. Press Ctrl+C to shut down.

但是,当访问 https://localhost:5001/api/values 时,浏览器显示“无法访问此站点(连接已重置)”的错误? enter image description here

用powershell测试

PS C:\work> Invoke-WebRequest https://localhost:5001/api/valuesInvoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.At line:1 char:1+ Invoke-WebRequest https://localhost:5001/api/values+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommandPS C:\work> Invoke-WebRequest http://localhost:5000/api/valuesInvoke-WebRequest : The remote server returned an error: (401) Unauthorized.At line:1 char:1+ Invoke-WebRequest http://localhost:5000/api/values+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Fiddler 返回以下消息。

fiddler.network.https> HTTPS handshake to localhost (for #3) failed. System.IO.IOException Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. < An existing connection was forcibly closed by the remote host

Internet Explorer 返回以下消息。

Turn on TLS 1.0, TLS 1.1, and TLS 1.2 in Advanced settings and try connecting to https://localhost:5001 again. If this error persists, it is possible that this site uses an unsupported protocol or cipher suite such as RC4 (link for the details), which is not considered secure. Please contact your site administrator.

最佳答案

您需要在 ACL 中添加/安装自签名证书,并且需要在本地主机的“受信任的根证书颁发机构”中添加。

或者

你可以评论 app.UseHttpsRedirection();在 launchSettings.json 中你需要有这样的东西:

"applicationUrl": "http://localhost:5000",

"iisExpress": {
"applicationUrl": "http://localhost:49845",
"sslPort": 0 //44382

关于c# - 基础连接已关闭。无法使用 HTTP.sys 运行 Asp.Net Core 2.1 Web API 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com