.net core 2.0 cookie 身份验证在尝试通过 https 访问时陷入无限重定向循环
.net core 2.0 cookie authentication getting stuck in infinite redirect loop when trying to access over https
我刚刚将我的代码移至我们使用 https 的 QA 环境,在 Dev 中工作的内容在 QA 中不起作用,因为浏览器陷入了无限重定向循环。我们的负载均衡器强制使用 https,因此当登录重定向从代码发生时,由于某种原因它试图重定向到 http 而不是 https,负载均衡器将停止它并再次添加 https,这会导致无限循环。我的问题是为什么这段代码不只是重定向到 https,路径在 ConfigureServices()
方法中是相对的。我在 fiddler 中查看过它,它确实为使用 http 而不是 https 的重定向添加了 FQDN。
是否有一些属性我需要添加到此处的选项以允许 https 重定向?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/LogIn";
options.LogoutPath = "/Account/LogOff";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
}
谢谢。
我们只使用:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
... //settings and logging initialization
app.Use((context, next) =>
{
context.Request.Scheme = "https";
return next();
});
... //all the rest middleware calls
}
它在 OWIN 和 .Net Core 2.0 以下的大多数情况下都有帮助
根据@Programmer 在对 OP 的评论中的建议,我看了一下:https://codeopinion.com/configuring-asp-net-core-behind-a-load-balancer/ 它准确地描述了我的情况(负载均衡器处的 ssl 终止和 .net core 2.0 应用程序重定向到http 登录)。然后我尝试使用文章建议的 header 通过 LB 发出请求,并在 Startup
class 的 Configure()
方法中添加这段代码:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
有趣的是,当我发出包含原型的请求时 header:
X-Forwarded-Proto:https
从 LB 外部,它将 header 传递给应用程序并且效果很好,不再有无限重定向循环。然而,当我们的基础架构人员将 header 添加到 LB 向 LB 后面的内部节点发出的请求时,我得到了一个重定向到 https,是的,但它也在重定向之前添加了 ip 地址 URL(我们有一个 netscaler LB)。显然,默认情况下,当您添加自定义 header 时,会有一个复选框将 IP 包含到内部节点并且必须取消选中。完成之后,我们就开始做生意了。
再次感谢@Programmer 的帮助。你绝对给我指明了正确的方向。
对于 .net core 2.1 及更高版本的 azure 身份验证,请尝试此代码。
services.Configure(AzureADDefaults.CookieScheme, options =>
{
options.Cookie.SameSite = SameSiteMode.None;
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
我刚刚将我的代码移至我们使用 https 的 QA 环境,在 Dev 中工作的内容在 QA 中不起作用,因为浏览器陷入了无限重定向循环。我们的负载均衡器强制使用 https,因此当登录重定向从代码发生时,由于某种原因它试图重定向到 http 而不是 https,负载均衡器将停止它并再次添加 https,这会导致无限循环。我的问题是为什么这段代码不只是重定向到 https,路径在 ConfigureServices()
方法中是相对的。我在 fiddler 中查看过它,它确实为使用 http 而不是 https 的重定向添加了 FQDN。
是否有一些属性我需要添加到此处的选项以允许 https 重定向?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/LogIn";
options.LogoutPath = "/Account/LogOff";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
}
谢谢。
我们只使用:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
... //settings and logging initialization
app.Use((context, next) =>
{
context.Request.Scheme = "https";
return next();
});
... //all the rest middleware calls
}
它在 OWIN 和 .Net Core 2.0 以下的大多数情况下都有帮助
根据@Programmer 在对 OP 的评论中的建议,我看了一下:https://codeopinion.com/configuring-asp-net-core-behind-a-load-balancer/ 它准确地描述了我的情况(负载均衡器处的 ssl 终止和 .net core 2.0 应用程序重定向到http 登录)。然后我尝试使用文章建议的 header 通过 LB 发出请求,并在 Startup
class 的 Configure()
方法中添加这段代码:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
有趣的是,当我发出包含原型的请求时 header:
X-Forwarded-Proto:https
从 LB 外部,它将 header 传递给应用程序并且效果很好,不再有无限重定向循环。然而,当我们的基础架构人员将 header 添加到 LB 向 LB 后面的内部节点发出的请求时,我得到了一个重定向到 https,是的,但它也在重定向之前添加了 ip 地址 URL(我们有一个 netscaler LB)。显然,默认情况下,当您添加自定义 header 时,会有一个复选框将 IP 包含到内部节点并且必须取消选中。完成之后,我们就开始做生意了。
再次感谢@Programmer 的帮助。你绝对给我指明了正确的方向。
对于 .net core 2.1 及更高版本的 azure 身份验证,请尝试此代码。
services.Configure(AzureADDefaults.CookieScheme, options =>
{
options.Cookie.SameSite = SameSiteMode.None;
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));