ASP Identity 2.0:重新生成身份

 洱冬橙66_156 发布于 2022-12-15 12:52

我无法让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是请求的一部分,不能在代码中更改?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有