【问题标题】:Add UserClaim with Identity asp.net core使用身份 asp.net 核心添加 UserClaim
【发布时间】:2018-11-15 19:42:43
【问题描述】:

在我的课堂上,我将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 表的方法是什么?问候

【问题讨论】:

    标签: c# asp.net-core .net-core asp.net-identity


    【解决方案1】:

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

    await _userManager.AddClaimAsync(user,claim)
    

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 2019-03-24
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多