我无法让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无效.
编辑
在查看链接的问题后,我尝试了以下(没有完整的错误处理):
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是请求的一部分,不能在代码中更改?