繁体   English   中英

添加带有Identity asp.net核心的UserClaim

[英]Add UserClaim with Identity asp.net core

在我的班级中,我将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

await _userManager.AddToRoleAsync(user, roleName);

我的问题是,向UserClaim表添加新寄存器的方法是什么? 问候

你几乎拥有一切,你只需要一个你要分配给用户的索赔。

await _userManager.AddClaimAsync(user,claim)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM