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

编程入门 行业动态 更新时间:2024-10-24 03:22:33
本文介绍了如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用 ASP.NET Core 2.x,我曾经能够通过简单地将它放在 UseUrls() 方法中来让 Kestrel 使用 HTTPS/SSL,如下所示:

var host = new WebHostBuilder().UseUrls("localhost", "111.111.111.111").UseKestrel().建造();

但现在我得到了例外:

System.InvalidOperationException:HTTPS 端点只能使用 KestrelServerOptions.Listen() 进行配置.

如何配置 Kestrel 以在 ASP.NET Core 2.x 中使用 SSL?

解决方案

基础知识.使用服务器 URL

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

WebHost.CreateDefaultBuilder(args).UseUrls("localhost:5000", "*:80").UseStartup().建造();

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

在 UseUrls() 方法的额外细节core/fundamentals/hosting?tabs=aspnetcore2x#server-urls" rel="noreferrer">此处为官方 Microsoft Docs.

但是,SSL 不适用于 UseUrls() 方法 --- 因此,这意味着如果您尝试添加 URL以 开头的程序会抛出异常

System.InvalidOperationException:HTTPS 端点只能使用 KestrelServerOptions.Listen() 进行配置.

端点配置.使用 HTTPS 并绑定 SSL 证书

HTTPS 端点只能使用 KestrelServerOptions 进行配置.

以下是使用 Listen 方法使用 TCP 套接字的示例:

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

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

您可以找到有关设置端点的更多信息 此处位于官方 Microsoft Docs.

如果您使用 IIS,IIS 的 URL 绑定会覆盖您通过调用 Listen 或 UseUrls 设置的任何绑定.有关详细信息,请参阅 Introduction toASP.NET 核心模块.

I am currently using ASP.NET Core 2.x and I used to be able to get Kestrel to to use HTTPS / SSL by simply putting it in the UseUrls() method like so:

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

But now I get the 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?

解决方案

The basics. Using Server URLs

If you want to associate your server to use all the IP addresses assigned to the server/web host then you can do this:

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

Note: The string format used in the UseUrls() method is: {ip address}:{port number}. - If you use an * (asterisks) for the IP address, that means all available IP address on the host. - The port number is not a requirement. If you leave it blank it will default to port 80.

There is a great amount of additional detail about the UseUrls() method over at the official Microsoft Docs here.

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

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

Endpoint configuration. Using HTTPS and binding a SSL certificate

HTTPS endpoints can only be configured using KestrelServerOptions.

Here is an example of using TCP sockets using the Listen method:

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();

Note: That if you use both the Listen method and UseUrls, the Listen endpoints override the UseUrls endpoints.

You can find more info about setting up endpoints 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.

更多推荐

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

本文发布于:2023-11-14 13:38:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   如何在   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!