我正在开发一个应用程序(ASP.NET MVC 5),我想防止特定页面上的身份验证 cookie 过期(这是一个巨大的表单,需要一些时间才能完全填写) )。我所拥有的是:
Startup.cs中的 ASP.NET 身份配置
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
validateInterval: TimeSpan.FromMinutes(15),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => Guid.Parse(id.GetUserId()))
},
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
});
SignIn Controller 中的方法实现
AuthenticationManager.SignIn(new AuthenticationProperties()
{
AllowRefresh = true,
IsPersistent = isPersistent,
ExpiresUtc = TimeSpan.FromMinutes(30)
}, identity);
jQuery 方法在特定页面上实现,每 10 分钟向服务器发送一次消息。
(function ($) {
function keepSessionAlive() {
$.post("/Resources/KeepSessionAlive");
}
// 10 minutes
setInterval(keepSessionAlive, 60 * 1000 * 10);
})(jQuery);
KeepSessionAlive 在 Controller 中实现
//
// POST: /Resources/KeepSessionAlive/
[HttpPost]
public JsonResult KeepSessionAlive()
{
if (HttpContext.Session != null)
HttpContext.Session["KeepSessionAlive"] = DateTime.Now;
return Json($"Last refresh {DateTime.Now.ToString("O")}");
}
问题: 当我导航到特定页面时,我可以看到以下发布请求:
- /Resources/KeepSessionAlive - 200 OK
- /Resources/KeepSessionAlive - 200 OK
- /Resources/KeepSessionAlive - 401 未经授权
但是 30 分钟后我收到 401 未经授权。我做错了什么?
顺便说一句。 CookieAuthenticationOptions.ExpireTimeSpan 和 AuthenticationProperties.ExpiresUtc 之间有什么区别。一定是一样的吧?如果我将它们设置为不同的值,它会如何表现?感谢您的澄清。
//编辑:
我发现cookie在15分钟后过期,等于validateInterval: TimeSpan.FromMinutes(15)
,但我认为它不会影响cookie过期,因为这是一个当您更改密码或向您的帐户添加外部登录时使用的安全功能
。
最佳答案
我不明白,但是当我将 CookieAuthenticationOptions.ExpireTimeSpan 和 AuthenticationProperties.ExpiresUtc 设置为相同的值(30 分钟)时,它就开始工作.
最终源码:
Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => Guid.Parse(id.GetUserId()))
}
});
登录
AuthenticationManager.SignIn(new AuthenticationProperties() {
IsPersistent = isPersistent }, identity);
jQuery
function keepSessionAlive() {
$.ajax({
type: "POST",
cache: false,
url: "/Resources/KeepSessionAlive",
success: function (result) {
console.debug("keepSessionAlive response [" + result + "]");
window.setTimeout(keepSessionAlive, 60 * 1000 * 15); // 15 minutes
}
});
}
keepSessionAlive();
关于jquery - ASP.NET Identity 2.0 防止身份验证 cookie 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805308/