I have added claims by using following code
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);
I need to update claim when someone update profile pic I need to update it how can I do that ?
I have tried couple of solution but nothing work .
When someone update profile pic than currently it has to logout and logged in again to see the effect.
CodePudding user response:
As you know the claims is stored inside the cookie when the signin.
So if you want to update the claims, you need to re-call the signin user codes with the updated claims.
Like the previous adding claims codes:
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);