我使用的是asp.net core 1.0,我有ASP身份。我想使用cookie来设置cookie过期时间,以便当页面空闲20分钟时间跨度时,它将重新路由到登录页面。目前它没有。 我的设置:
app.UseIdentity();
将身份添加为:
services.AddIdentity<CRAMSUser, IdentityRole>(config =>
{
config.User.RequireUniqueEmail = true;
config.Password.RequiredLength = 8;
config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == (int)HttpStatusCode.OK)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
}
return Task.FromResult(0);
}
};
})
.AddEntityFrameworkStores<CRAMSContext>()
.AddDefaultTokenProviders();
如何设置它以便在闲置一小时后重新路由到登录页面?
答案 0 :(得分:3)
使用滑动过期:请参阅docs
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
Cookie上的滑动过期以这种方式工作:在身份验证时,设置绝对过期时间(即现在+ 60Min)。在后续请求中,应将到期时间重置为新的Now + 60min,但不会对每个请求进行此更新,因为这会浪费带宽。因此,只有在滑动过期的X%超过之后,才会使用新的过期日期更新cookie。根据文档,ASP.NET X占ExpirationTimeSpan
(30分钟)的50%。