我的 MVC 6 ASP.NET 5 项目中有以下设置:
配置方法中的Startup.cs:
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "Cookie";
options.LoginPath = new PathString("/<TENANT>/account/signin/");
options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/");
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.Events = new CookieAuthenticationEvents
{
OnRedirectToReturnUrl = MyClass.RedirectToReturnUrlAsync
};
});
活动类:
public static class MyClass
{
public static async Task RedirectToReturnUrlAsync(CookieRedirectContext context)
{
context.Options.LoginPath = new PathString("/<HERE I PLAN TO PUT LOGIC TO FIGURE OUT TENANT FROM CONTEXT>/account/signin");
}
}
假设用户访问以下网址:
http://localhost/mycompany/securecontroller/secureaction
我希望 Cookie 中间件将用户重定向到:
http://localhost/mycompany/account/signin
问题是当发生重定向到 Return Url 时,代码“MyClass.RedirectToReturnUrlAsync”永远不会被命中,因此我找不到在运行时修改 LoginPath 的机会。
我怀疑我的设置有问题。有人遇到过这个问题吗?
Hooroo
好吧,我想我明白了。我从错误的角度看待问题(并且是在睡了一觉之后!)
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "Cookie";
options.LoginPath = new PathString("/<TENANT>/account/signin/");
options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/");
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.Events = new MyCookieAuthenticationEvents();
});
创建您自己的自定义 Cookie 身份验证事件的正确方法是从 CookieAuthenticationEvents 对象派生并覆盖您想要自定义的事件,如下所示:
public class MyCookieAuthenticationEvents : CookieAuthenticationEvents
{
public override Task RedirectToLogin(CookieRedirectContext context)
{
context.RedirectUri = <PUT LOGIC HERE TO REPLACE YOUR REDIRECT URI>
return base.RedirectToLogin(context);
}
}
我在之前的尝试中也瞄准了错误的事件。就我而言,正确的重写方法是“RedirectToLogin”方法。
Hooroo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)