gpt4 book ai didi

ssl - 如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用?

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:22 25 4
gpt4 key购买 nike

我目前使用的是 ASP.NET Core 2.x,我曾经能够让 Kestrel 使用 HTTPS/SSL,只需将它放在 UseUrls() 方法中,如下所示:

var host = new WebHostBuilder()
.UseUrls("http://localhost", "https://111.111.111.111")
.UseKestrel()
.Build();

但现在我得到了异常(exception):

 System.InvalidOperationException:
HTTPS endpoints can only be configured using KestrelServerOptions.Listen().

How do I configure Kestrel to use SSL in ASP.NET Core 2.x?

最佳答案

基础知识。使用服务器 URL

如果您想关联您的服务器以使用分配给服务器/网络主机的所有 IP 地址,那么您可以这样做:

WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5000", "http://*:80")
.UseStartup<Startup>()
.Build();

注意:UseUrls() 方法中使用的字符串格式为:http://{ip 地址}:{端口号}
- 如果您使用 *(星号)作为 IP 地址,则表示主机上所有可用的 IP 地址。
- 端口号不是必需的。如果将其留空,它将默认为端口 80。

the official Microsoft Docs here 上有大量关于 UseUrls() 方法的额外细节。 .

However, SSL will not work with the UseUrls() method --- so, that means if you try to add a URL starting with https:// the program will throw the exception

System.InvalidOperationException:
HTTPS endpoints can only be configured using KestrelServerOptions.Listen().

端点配置。使用HTTPS并绑定(bind)SSL证书

HTTPS 端点只能使用 KestrelServerOptions 配置。

下面是一个使用 Listen 方法使用 TCP 套接字的例子:

WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000); // http:localhost:5000
options.Listen(IPAddress.Any, 80); // http:*:80
options.Listen(IPAddress.Loopback, 443, listenOptions =>
{
listenOptions.UseHttps("certificate.pfx", "password");
});
})
.UseStartup<Startup>()
.Build();

注意:如果您同时使用 Listen 方法和 UseUrlsListen 端点会覆盖 UseUrls端点。

您可以找到有关设置端点的更多信息 here at the official Microsoft Docs .

If you use IIS, the URL bindings for IIS override any bindings that you set by calling either Listen or UseUrls. For more information, see Introduction to ASP.NET Core Module.

关于ssl - 如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46621788/

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