Cookie 身份验证过期不会重新路由到登录页面
Cookie Authentication expiration does not re-route to login page
我正在使用 asp.net 核心 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();
如何设置它以便在闲置一个小时后重新路由到登录页面?
使用滑动到期:见docs
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
cookie 的滑动过期方式如下:在身份验证时,设置绝对过期时间(即现在+60 分钟)。在后续请求中,过期时间应重置为新的 Now+60min,但不会对每个请求都进行更新,因为那样会浪费带宽。因此,只有在滑动过期的 X% 之后,cookie 才会更新为新的过期日期。根据文档,ASP.NET X 是 ExpirationTimeSpan
(30 分钟)的 50%。
我正在使用 asp.net 核心 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();
如何设置它以便在闲置一个小时后重新路由到登录页面?
使用滑动到期:见docs
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
cookie 的滑动过期方式如下:在身份验证时,设置绝对过期时间(即现在+60 分钟)。在后续请求中,过期时间应重置为新的 Now+60min,但不会对每个请求都进行更新,因为那样会浪费带宽。因此,只有在滑动过期的 X% 之后,cookie 才会更新为新的过期日期。根据文档,ASP.NET X 是 ExpirationTimeSpan
(30 分钟)的 50%。