How to hold the cookies claims updated with MCV5/OWIN

浪子不回头ぞ 提交于 2019-12-03 13:23:30

We added this feature to 2.0, here's how you would configure the CookieMiddleware to get fresh claims every 30 minutes (regenerateIdentity should call into the code that you use to generate the ClaimsIdentity for users when they sign in, and validateInterval controls how often regenerateIdentity is called, but only if the old cookie is still valid)

        app.UseCookieAuthentication(new CookieAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

Nope, this is in essence what you would have to do if you have the requirement that the cookie be 100% up to date at all times.

The Katana cookie middleware options class has a OnValidateIdentity callback for every time the cookie is presented -- you could check in there and then re-issue the cookie. This is how the ASP.NET Identity 2.0 bits work to expire a cookie if a user's password was changed since the cookie was issued.

Another option might be to use SignalR in a design where logged in user browsers are directed to reacquire the cookie via an Ajax call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!