在ASP.net和C#中,需要中间件来重定向到HTTPS吗?

14 浏览
0 Comments

在ASP.net和C#中,需要中间件来重定向到HTTPS吗?

如何推荐将所有不安全的传入请求重定向到HTTPS?我需要编写一个中间件组件吗?如果需要,我无法弄清楚如何获得服务器名称。

public class RedirectHttpMiddleware
{
    RequestDelegate _next;
    public RedirectHttpMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task Invoke(HttpContext context)
    {
        if (context.Request.IsSecure)
            await _next(context);
        else
        {
            var server = "";  // 如何获得服务器名称?
            context.Response.Redirect("https://" + server + context.Request.Path);
        }
    }
}

0