Answer the question
In order to leave comments, you need to log in
How to get and set Claim for arbitrary users in Asp.Net Core Indetity?
I can not find a clear example of how you can get Claims for an arbitrary user and how you can set it. This is not about the current user.
For example, how do you list all users and all the claims they have installed? Second example, how to create, edit and delete claims for an arbitrary application user?
Answer the question
In order to leave comments, you need to log in
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>,
AdditionalUserClaimsPrincipalFactory>();
public class AdditionalUserClaimsPrincipalFactory
: UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
public AdditionalUserClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor)
{ }
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
var identity = (ClaimsIdentity)principal.Identity;
var claims = new List<Claim>();
claims.Add(user.IsAdmin ? new Claim(JwtClaimTypes.Role, "admin") : new Claim(JwtClaimTypes.Role, "user"));
identity.AddClaims(claims);
return principal;
}
}
public class ApplicationUser : IdentityUser
{
[PersonalData]
public string Name { get; set; }
[PersonalData]
public DateTime BirthDate { get; set; }
public bool IsAdmin { get; set; }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question