监听 HTTP 和 HTTPS? - C#/ASP.NET/Kestrel

Listening on HTTP and HTTPS? - C#/ASP.NET/Kestrel

问题 - 我需要我的应用程序强制 HTTPS 连接 (443),但是如果连接请求在 80 上出现,接受它并接受它,尽管在我的下面的代码中我得到了

IApplicationBuilder does not contain a definition for UseHttpRedirection" and no accessible extension method 'UseHttpRedirection' accepting first argument of type IA ApplicationBuilder could be found, and error HttpRequest does not contain a definition for port.

我做错了什么?

        // Always require HTTPS
        app.UseHttpsRedirection();
        //Use when requiring port 80
        app.UseWhen(context => context.Request.Port == 80, httpApp =>
        {
            httpApp.UseHttpRedirection();
        });

嗯,没有方法UseHttpRedirection提供的唯一重定向是从 http 到 https

What am I doing wrong?

您可以包含 https 重定向,这很好。如果将服务器绑定到端口 80,那也可以使用 https。 如果您发现中间件不适合您的用例,请查看并复制它:https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/HttpsPolicy/src/HttpsRedirectionMiddleware.cs