【问题标题】:Asp.net 5.0 User Has Role in ViewAsp.net 5.0 用户在视图中具有角色
【发布时间】:2021-05-03 22:17:42
【问题描述】:

有人可以帮助我了解它在 Asp.net 中的工作原理吗?如何在 Razor 视图中检查用户(未登录)的角色?

假设这样的情况: 在控制器中,我得到了所有用户的列表:

var Users = await _context.ApplicationUser.ToListAsync()
    return View(Users);

在视图中:

@foreach ( var user in Users){
if (user.IsInRole("Admin")) <---- check here if the user is in role
    { 
       do something
    }
}

如何检查是否为每个用户分配了特定角色?

【问题讨论】:

    标签: asp.net asp.net-core authentication roles


    【解决方案1】:

    您需要在构造函数中注入 usermanager。然后做

     await _userManager.IsInRoleAsync(user, role.Name); 
    

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2017-07-30
      • 2015-01-09
      • 2012-10-11
      • 2017-12-11
      • 2016-10-13
      相关资源
      最近更新 更多