【问题标题】:My user isn't logged out after the session expires using asp.net identity 2.0使用 asp.net identity 2.0 会话过期后我的用户没有注销
【发布时间】:2015-08-14 12:03:56
【问题描述】:

我有一个使用 Identity 2 进行身份验证的 MVC 应用程序。登录后,如果我关闭浏览器然后再次打开应用程序,则会出现 3 个问题。

  1. 用户未重定向到登录页面
  2. 会话仍然包含声明中的一些用户详细信息
  3. 会话缺少声明中不属于身份框架的其他自定义信息

我正在使用 IIS 在 Windows Server 上运行应用程序,但我可以在本地开发环境中重现该问题

在我调试问题时,cookie 和服务器上的会话都设置为在 1 分钟后过期

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString(url.Action("LogIn","Auth")),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
                        validateInterval: TimeSpan.FromMinutes(1),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },
                CookieName = "MyApplication"
            });

【问题讨论】:

  • 您确定强制用户每分钟注销一次,即使他们处于活动状态是个好主意吗?
  • 不,这不是我在调试问题的时候。修复后我会将其设置为 30 分钟
  • 为什么你认为会话过期会导致身份验证过期,他们使用单独的cookie。
  • 即便如此,为什么?您应该使用会话存储 - 它在整个会话期间都可用(当用户处于活动状态且尚未离开网站时)并在用户空闲或离开网站时被删除
  • 我只为应用程序使用了 1 个 cookie。我不需要存储很多关于用户的信息,只需要存储角色、他们有权访问并经过身份验证的区域。这些区域作为声明添加到 cookie,类似于 Identity 处理多个角色的方式。我需要查看会话存储,但我的工作正常。我已经解决了到期问题,我会发布答案

标签: c# asp.net asp.net-mvc session asp.net-identity-2


【解决方案1】:

问题是我从未将 cookie 设置为过期,添加以下 2 行解决了我遇到的问题

SlidingExpiration = true, 
ExpireTimeSpan = TimeSpan.FromMinutes(30)

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString(url.Action("LogIn","Auth")),
        Provider = new CookieAuthenticationProvider
        {
            // Enables the application to validate the security stamp when the user logs in.
            // This is a security feature which is used when you change a password or add an external login to your account.  
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        },
        CookieName = "MyApplication", 
        SlidingExpiration = true, 
        ExpireTimeSpan = TimeSpan.FromMinutes(30)
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多