如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用? [英] How to use HTTPS / SSL with Kestrel in ASP.NET Core 2.x?

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

问题描述

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

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

但现在我得到了例外:

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

<小时><块引用>

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

解决方案

基础知识.使用服务器 URL

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

WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:5000", "http://*:80").UseStartup<启动>().建造();

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

此处为官方 Microsoft Docs.

<块引用>

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

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 方法和 UseUrlsListen 端点会覆盖 UseUrls端点.

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

<块引用>

如果您使用 IIS,IIS 的 URL 绑定会覆盖您通过调用 ListenUseUrls 设置的任何绑定.有关详细信息,请参阅 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("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

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

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