如何在ASP.NET Core 2.x中使用带有Kestrel的HTTPS / SSL? [英] How to use HTTPS / SSL with Kestrel in ASP.NET Core 2.x?

查看:2526
本文介绍了如何在ASP.NET Core 2.x中使用带有Kestrel的HTTPS / SSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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:
只能使用KestrelServerOptions.Listen()配置HTTPS端点。







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



解决方案

基础知识。使用服务器URL



如果要将服务器关联以使用分配给服务器/ Web主机的所有IP地址,则可以执行以下操作:

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

注意: UseUrls()<中使用的字符串格式/ code>方法是: http:// {ip address}:{port number}

- 如果你使用 * (星号)表示IP地址,即主机上所有可用的IP地址。

- 端口号不是必需的。如果您将其留空,则默认为端口80.



有关的大量其他详细信息UseUrls()方法在此处的官方Microsoft文档


然而, SSL无法使用 UseUrls() 方法 ---所以,这意味着如果您尝试添加以 https:// 程序将抛出异​​常

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







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




重要提示:

托管<建议不要使用Kestrel强健>公共端点(即使使用SSL),您应该使用反向代理(如Nginx或IIS)等技术,而不是在野外暴露Kestrel。

---您可以从官方Microsoft Docs


只能使用 KestrelServerOptions 配置HTTPS端点。



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



'pre> WebHost.CreateDefaultBuilder(参数)
.UseKestrel(选项=>
{
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, 密码);
});
})
.UseStartup<启动>()
.Build();

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



您可以找到有关设置端点的更多信息这里是官方的Microsoft Docs



< blockquote>

如果您使用IIS,IIS的URL绑定将覆盖您通过调用 Listen 或UseUrls设置的任何绑定。有关详细信息,请参阅 ASP.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("http://localhost", "https://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("http://localhost:5000", "http://*:80")
    .UseStartup<Startup>()
    .Build();

Note: The string format used in the UseUrls() method is: http://{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 https:// 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

IMPORTANT NOTE:
Hosting over a public endpoint via Kestrel (even with SSL) is not recommended and you should use technologies like reverse proxies (such as Nginx or IIS) instead exposing Kestrel out in the wild.
--- You can read more about this from the official Microsoft Docs here.

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中使用带有Kestrel的HTTPS / SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆