使用AddCookie的AddAuthentication重定向到http而不是https

时间:2019-01-29 21:59:30

标签: asp.net-core

我们有一个ASP.Net Core网站,并使用AddAuthentication和AddCookie来在用户未登录时将其重定向到OAuth登录提供程序。我们可以使用LoginPath提供登录操作的相对路径。这必须是相对路径;完整路径会导致异常。问题在于我们的网站位于负载均衡器后面。到负载均衡器的流量为https,但从那里变为http到达Web服务器。这将导致重定向URL为http,而不是https。我们如何控制LoginPath使用https作为重定向?

1 个答案:

答案 0 :(得分:0)

我在@GabrielLuci推荐的帖子中找到了答案。

ASP.NET Core CookieAuthenticationOptions.LoginPath on different domain

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
    o.Cookie.Name = "myCookie";
    o.Events = new CookieAuthenticationEvents()
    {
        OnRedirectToLogin = (context) =>
        {
            context.HttpContext.Response.Redirect("https://externaldomain.com/login");
            return Task.CompletedTask;
        }
    };
});