ASP Identity 2.0:重新生成身份

我无法让ASP Identity按需刷新存储在cookie中的身份。

Startup.Auth.cs文件中,cookie设置为重新生成,如下所示:

 app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentityCallback: ((manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)), getUserIdCallback: ((claimsIdentity) => int.Parse(claimsIdentity.GetUserId()))) } }); 

但是我无法弄清楚如何在代码中刷新User.Identity上的内容,即在我需要刷新时强制刷新身份cookie。

我希望能够以编程方式使用重新生成身份回调,这可能吗?

我的问题与此类似: 如何使用Asp.Net Identity 2将用户添加到角色后使.AspNet.ApplicationCookie无效?

但是我想刷新而不是使cookie无效。

编辑


在查看链接的问题后,我尝试了以下(没有完整的error handling):

 IOwinContext context = Request.GetOwinContext(); QuizSparkSignInManager manager = context.Get(); ClaimsIdentity newIdentity = manager.CreateUserIdentity(manager.UserManager.FindById(User.Identity.GetUserId())); AuthenticateResult authenticationContext = await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie); if (authenticationContext != null) { context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant( newIdentity, authenticationContext.Properties); } bool first2 = User.IsInRole("Turtle"); 

Edit2:但是用户似乎仍然没有刷新。 在页面重新加载它们似乎刷新,我是正确的认为这是因为User.Identity cookie是请求的一部分,不能在代码中更改?

如果您尝试向已登录的用户添加新角色,则需要签署用户。 然后使用新角色创建新身份,并使用新身份为用户签名。 这是更新cookie的唯一方法。

检查用户属性是否已更改的最佳位置是您已使用的回调: CookieAuthenticationProvider.OnValidateIdentity 。 像这样的东西。

 app.UseCookieAuthentication(new CookieAuthenticationOptions { // other stuff Provider = new CookieAuthenticationProvider { // this function is executed every http request and executed very early in the pipeline // and here you have access to cookie properties and other low-level stuff. // makes sense to have the invalidation here OnValidateIdentity = async context => { // invalidate user cookie if user's security stamp have changed var invalidateBySecirityStamp = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)); await invalidateBySecirityStamp.Invoke(context); if (context.Identity == null || !context.Identity.IsAuthenticated) { return; } if(/*Need to update cookie*/) { // get user manager. It must be registered with OWIN var userManager = context.OwinContext.GetUserManager(); var username = context.Identity.Name; // get new user identity with updated properties var updatedUser = await userManager.FindByNameAsync(username); // updated identity from the new data in the user object var newIdentity = updatedUser.GenerateUserIdentityAsync(manager); // kill old cookie context.OwinContext.Authentication.SignOut(context.Options.AuthenticationType); // sign in again var authenticationProperties = new AuthenticationProperties() { IsPersistent = context.Properties.IsPersistent }; context.OwinContext.Authentication.SignIn(authenticationProperties, newIdentity); } } } }); 

免责声明 – 从未测试过,甚至没有尝试过编译它。

也可以看到我的其他答案供参考 – 几乎相同的代码片段,但不同的目标。

UPD:关于问题的另一部分 – 如何检测角色变化:
我可以想到一种方法 – 在用户记录上有另一个GUID。 与SecurityStamp类似,但框架未使用。 称之为MySecurityStamp 。 登录时将MySecurityStamp值添加到cookie作为声明。 在每个请求中,将Cookie中的MySecurityStamp值与数据库中的值进行比较。 如果值不同 – 重新生成标识的时间。 在添加/删除的每个新角色上,为数据库中的用户修改MySecurityStamp 。 这将涵盖所有浏览器中的所有会话。