如何为.NET的HttpListener配置SSL与HTTPS?配置遇阻求助
.NET HttpListener配置HTTPS失败问题
我在给.NET服务器配置SSL和HTTPS时遇到了问题,以下是当前的Main方法代码:
static async Task Main() { var dbContext = new DatabaseContext(); var databaseService = new DatabaseService(dbContext); HttpListener listener = new() { AuthenticationSchemes = AuthenticationSchemes.None, Prefixes = { "https://*:8080/", "http://*:8083/" } }; var certificate = new X509Certificate2("./localhost.pem"); listener.Start(); Console.WriteLine("Listening for HTTP and HTTPS connections..."); while (true) { HttpListenerContext context = await listener.GetContextAsync(); Console.WriteLine("got it"); if (context.Request.IsWebSocketRequest) { await WebSocketRequest.ProcessWebSocketRequest(context, databaseService); } else { if (context.Request.HttpMethod == "OPTIONS") { await WriteResponse.WriteOptionsResponse(context); } else { await HttpRequests.ProcessRestRequest(context, databaseService); } } } }
我使用mkcert工具执行mkcert localhost命令生成证书,并将其添加到构建目录中。证书加载似乎没有问题,我可以打印证书数据。但无论是React应用还是Postman都无法向服务器发起HTTPS请求:HTTP请求到8083端口正常,但HTTPS请求到8080端口失败。Postman报错ECONNRESET,React提示Failed to load resource: An SSL error has occurred and a secure connection to the server cannot be made.
更新
感谢所有回复,最终我切换到了ASP.Net。
内容的提问来源于stack exchange,提问作者eskil4152
相关产品推荐
相关产品推荐

