我想知道如何正确处理cookie过期的情况?是否可以执行自定义操作?
我想要实现的是,当 cookie 过期时,从当前 cookie 中取出一些信息,并通过该信息重定向到操作参数。是否可以 ?
没有一个好的方法可以实现这一点。如果 cookie 过期,则不会将其发送到服务器以提取任何信息。使用 ASP.Net Core Identity,您对此没有太多控制权。这样您就可以使用 Cookie 中间件了。
当 cookie 过期时,这为用户提供了正常的重定向:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/Home/Index");
});
}
实现您所寻找的效果的最佳方法是将 cookie 过期设置为晚于真正的用户会话过期,然后执行会话过期服务器端并在此时重定向用户。虽然这并不理想,但当 cookie 过期时您没有其他选择。
public void ConfigureServices(IServiceCollection services)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
// Redirect when cookie expired or not present
LoginPath = new PathString("/Account/Unauthorized/"),
AutomaticAuthenticate = true,
// never expire cookie
ExpireTimeSpan = TimeSpan.MaxValue,
Events = new CookieAuthenticationEvents()
{
// in custom function set the session expiration
// via the DB and reset it everytime this is called
// if the session is still active
// otherwise, you can redirect if it's invalid
OnValidatePrincipal = <custom function here>
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)