添加具有身份 asp.net 核心的 UserClaim

Add UserClaim with Identity asp.net core

在我的 class 中,我将 ApplicationRoleApplicationUser 称为:

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;
}

我有一种方法可以为用户分配角色,例如:

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();
}

换句话说。我用这个

添加新寄存器到 UserRole table
await _userManager.AddToRoleAsync(user, roleName);

我的问题是,UserClaim table 添加新寄存器的方法是什么?此致

您几乎拥有一切,只需要分配给用户的声明即可。

await _userManager.AddClaimAsync(user,claim)