使用 ASP 身份进行会话和用户身份验证

Session and user authentication using ASP Identity

我在 MVC 项目中使用 Asp Identity for Authentication,我的问题是会话结束并且在某些情况下用户仍然在进行身份验证。

我在配置时将 OWIN 的超时设置为 20 分钟

 app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                ExpireTimeSpan = TimeSpan.FromMinutes(20),
                SlidingExpiration = false,
            })

20 分钟后我的会话数据和 Owin 被清除。

但在某些情况下仍然存在问题:

  1. 当 IIS 重置会话时清除但用户仍然通过身份验证。

  2. 当我在 vs 2015 上构建我的解决方案时,会话清除但用户仍然经过身份验证。

您正在使用 DefaultAuthenticationTypes.ApplicationCookie - cookie 身份验证。

这是意料之中的事。cookie 将存在 20 分钟(根据您的配置)然后过期。与Session

无关

如果您真的想将您的身份验证与会话相关联,请参见此处:

虽然我建议使用 ActionFilter 代替基础 class(如评论之一所述)