ASP.NET Core中如何更新声明

3
我已使用以下代码添加了声明:

我已使用以下代码添加了声明

var claims = new List<Claim>
                 {
                    new Claim(Constants.ClaimTypes.BUSINESS_ID, user.BusinessID.ToString()),
                    new Claim(Constants.ClaimTypes.NAME, user.FullName),
                    new Claim(Constants.ClaimTypes.IMAGE, user.ProfileUrl ?? user.LogoUrlEn ?? user.LogoUrlEn ?? ""),
                    new Claim(Constants.ClaimTypes.EMAIL, user.Email),
                    new Claim(Constants.ClaimTypes.USER_ID, user.UserID.ToString()),
                    new Claim(Constants.ClaimTypes.ROLE, user.RoleID.ToString()),
                    new Claim(Constants.ClaimTypes.RIGHTS, string.Join(',', user.RolesRights.Select(S => $"{S.EntityName}|{S.EntityID}|{S.RightID}")))
                };

var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

var authProperties = new AuthenticationProperties
        {
            AllowRefresh = true,
            IsPersistent = true,
            RedirectUri = "/Authentication/Login"
        };

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                              new ClaimsPrincipal(claimsIdentity),
                              authProperties);

当有人更新个人资料照片时,我需要更新认领信息,我该如何做?

我尝试了几种解决方案,但都没有成功。

目前,当有人更新个人资料照片时,必须注销并重新登录才能看到效果。

1个回答

4

众所周知,当用户登录时,声明存储在cookie中。

因此,如果您想要更新声明,需要使用更新后的声明重新调用登录用户代码。

就像之前添加声明的代码一样:

var claims = new List<Claim>
                 {
                    new Claim(Constants.ClaimTypes.BUSINESS_ID, user.BusinessID.ToString()),
                    new Claim(Constants.ClaimTypes.NAME, user.FullName),
                    new Claim(Constants.ClaimTypes.IMAGE, user.ProfileUrl ?? user.LogoUrlEn ?? user.LogoUrlEn ?? ""),
                    new Claim(Constants.ClaimTypes.EMAIL, user.Email),
                    new Claim(Constants.ClaimTypes.USER_ID, user.UserID.ToString()),
                    new Claim(Constants.ClaimTypes.ROLE, user.RoleID.ToString()),
                    new Claim(Constants.ClaimTypes.RIGHTS, string.Join(',', user.RolesRights.Select(S => $"{S.EntityName}|{S.EntityID}|{S.RightID}")))
                };

var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

var authProperties = new AuthenticationProperties
        {
            AllowRefresh = true,
            IsPersistent = true,
            RedirectUri = "/Authentication/Login"
        };

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                              new ClaimsPrincipal(claimsIdentity),
                              authProperties);

1
它有效!!但只想确认是否没有更新索赔的方法? - Faizan Naeem
2
没错。声明保存在客户端的 cookie 中。如果你想要更新,就需要重新更新 cookie。 - Brando Zhang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接