如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用?
How to use HTTPS / SSL with Kestrel in ASP.NET Core 2.x?
我目前正在使用 ASP.NET Core 2.x 并且我曾经能够让 Kestrel 使用 HTTPS / SSL 只需将其放入 UseUrls()
方法中,如下所示:
var host = new WebHostBuilder()
.UseUrls("http://localhost", "https://111.111.111.111")
.UseKestrel()
.Build();
但现在我得到了例外:
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
如果您想关联您的服务器以使用分配给 server/web 主机的所有 IP 地址,那么您可以这样做:
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5000", "http://*:80")
.UseStartup<Startup>()
.Build();
注:UseUrls()
方法中使用的字符串格式为:http://{ip address}:{port number}
.
- 如果您使用 *
(星号)作为 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 并绑定 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
方法和 UseUrls
,Listen
端点将覆盖 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.
您不需要单独使用 kestrel 实现 https。如果您 运行ning 一个需要 https 的应用程序,它很可能会面向外部互联网。这意味着您需要 运行 kestrel 支持 nginx 或 Apache,并让其中之一为您处理 https 请求。
我目前正在使用 ASP.NET Core 2.x 并且我曾经能够让 Kestrel 使用 HTTPS / SSL 只需将其放入 UseUrls()
方法中,如下所示:
var host = new WebHostBuilder()
.UseUrls("http://localhost", "https://111.111.111.111")
.UseKestrel()
.Build();
但现在我得到了例外:
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
如果您想关联您的服务器以使用分配给 server/web 主机的所有 IP 地址,那么您可以这样做:
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5000", "http://*:80")
.UseStartup<Startup>()
.Build();
注:UseUrls()
方法中使用的字符串格式为:http://{ip address}:{port number}
.
- 如果您使用 *
(星号)作为 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 withhttps://
the program will throw the exceptionSystem.InvalidOperationException: HTTPS endpoints can only be configured using KestrelServerOptions.Listen().
端点配置。使用 HTTPS 并绑定 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
方法和 UseUrls
,Listen
端点将覆盖 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
orUseUrls
. For more information, see Introduction to ASP.NET Core Module.
您不需要单独使用 kestrel 实现 https。如果您 运行ning 一个需要 https 的应用程序,它很可能会面向外部互联网。这意味着您需要 运行 kestrel 支持 nginx 或 Apache,并让其中之一为您处理 https 请求。