使用 MVC 6 ASP.NET View 中的用户角色列表
本文关键字:用户 角色 列表 View MVC ASP NET 使用 | 更新日期: 2023-09-27 18:35:32
如何在视图中获取当前用户角色的列表?
我试过了
@inject UserManager<ApplicationUser> userManager
@userManager.GetRolesAsync(new ApplicationUser() {UserName = User.GetUserName()})
但它不起作用,总是空列表。
我找到了答案:
@inject UserManager<ApplicationUser> userManager
<ul>
@foreach (var role in await userManager.GetRolesAsync(await userManager.FindByNameAsync(User.Identity.Name)))
{
<li>@role</li>
}
</ul>