2

In my class I call ApplicationRole and ApplicationUser as:

private readonly RoleManager<ApplicationRole> _roleManager;
private readonly UserManager<ApplicationUser> _userManager;
public Guid RoleId { get; set; }
public Guid UserId { get; set; }

public AssignRoleToUserModel(
    RoleManager<ApplicationRole> roleManager,
    UserManager<ApplicationUser> userManager)
{
    _roleManager = roleManager;
    _userManager = userManager;
}

And I have a method to assign roles to users like:

public async Task<IActionResult> OnPost(RoleToUserModel model)
{
    var roleId = model.RoleId.ToString();
    var userId = model.UserId.ToString();
    var role = await _roleManager.FindByIdAsync(roleId);
    var roleName = role.Name;
    var user = await _userManager.FindByIdAsync(userId);
    await _userManager.AddToRoleAsync(user, roleName);
    return  RedirectToPage();
}

So in other words. I add new register to UserRole table with this

await _userManager.AddToRoleAsync(user, roleName);

My question is, what is the method to add new register to UserClaim table? Regards

1 Answer 1

2

You have almost everything, you only need a claim you are going to assign to the user.

await _userManager.AddClaimAsync(user,claim)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.