【问题标题】:ASP.NET Identity 2.0 prevent auth cookie expirationASP.NET Identity 2.0 防止 auth cookie 过期
【发布时间】:2016-10-14 18:57:59
【问题描述】:

我正在开发一个应用程序 (ASP.NET MVC 5),我想在该应用程序中防止特定页面上的 auth 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 控制器中的方法实现

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 在控制器中的实现

//
// POST: /Resources/KeepSessionAlive/
[HttpPost]
public JsonResult KeepSessionAlive()
{
    if (HttpContext.Session != null)
        HttpContext.Session["KeepSessionAlive"] = DateTime.Now;
    return Json($"Last refresh {DateTime.Now.ToString("O")}");
}

问题: 当我导航到特定页面时,我可以看到以下发帖请求:

  1. /Resources/KeepSessionAlive - 200 OK
  2. /Resources/KeepSessionAlive - 200 OK
  3. /Resources/KeepSessionAlive - 401 未授权

但 30 分钟后,我得到了未经授权的 401。我做错了什么?

顺便说一句。 CookieAuthenticationOptions.ExpireTimeSpanAuthenticationProperties.ExpiresUtc 有什么区别。应该一样吧?如果我将它们设置为不同的值,它的行为如何?感谢您的澄清。

// 编辑:

我发现cookie在等于validateInterval: TimeSpan.FromMinutes(15)的15分钟后过期,但我认为它不会影响cookie过期,因为this is a security feature which is used when you change a password or add an external login to your account

【问题讨论】:

    标签: jquery asp.net asp.net-mvc cookies asp.net-identity


    【解决方案1】:

    我不明白,但是当我将 CookieAuthenticationOptions.ExpireTimeSpanAuthenticationProperties.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();
    

    【讨论】:

    • 我遇到了类似的问题,我的 .aspnet.applicationcookie 没有刷新它的过期时间,但是将 SlidingExpiration CookieAuthenticationOptions' 设置为 true 为我解决了这个问题。
    猜你喜欢
    • 2015-08-09
    • 2018-06-03
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多