【问题标题】:How to check user roles in View MVC5如何在 View MVC5 中检查用户角色
【发布时间】:2018-02-07 18:32:04
【问题描述】:

正如问题所说,我正在尝试在 MVC5 视图中检查用户对象的角色。基本上我的观点是列出所有注册用户及其当前角色。

这是我的看法:

    @model IEnumerable<IconicMx.CandidateDatabase.Models.ApplicationUser>

@{
    ViewBag.Title = "Talent Center Institute: Usuarios";
    ViewBag.Menu = "Usuarios";
    ViewBag.Submenu = "Lista de usuarios";
    ViewBag.MenuFaClass = "fa-cog";
    ViewBag.InitialPanel = true;
}

<h2>Lista de usuarios</h2>

<p>
    @Html.ActionLink("Crear usuario", "Register", "Account", null, new { @class = "btn btn-primary" })
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            Tipo de usuario
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @if (ViewBag.manager.IsInRole(item.Id, "Admin"))
            {
                <text>Administrador</text>
            }
            else
            {
                <text>Capturista</text>
            }
        </td>
        <td>
            @Html.ActionLink("Editar", "Edit", new { id=item.Id }, new { @class = "btn btn-primary btn-xs" }) |
            @Html.ActionLink("Eliminar", "Delete", new { id=item.Id }, new { @class = "btn btn-primary btn-xs" })
        </td>
    </tr>
}

</table>

这是我的控制器:

 public ActionResult Index()
    {
        ViewBag.manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
        return View(db.Users.ToList());
    }

我正在尝试使用 UserManager 的 IsInRole 函数,但在渲染视图时出现运行时错误,提示该函数未定义!!如果我在控制器中调用此函数,它会按预期运行。

注意:我不是想在会话中获取当前登录的用户!! (User.identity.IsInRole 没有帮助)

【问题讨论】:

  • 我认为最好将此信息放入视图模型并以这种方式将信息传递给视图,而不是尝试访问视图本身中的IUserManager 实例。分离关注点:为控制器中的视图准备数据,并与视图一起显示数据。

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-identity


【解决方案1】:

首先,从控制器中的数据库中取出数据并将数据传递到视图中,而不是对服务类的引用。这将为您省去很多麻烦。 ViewBag 可以很好地传递一个奇怪的 serialiseable 对象,但在传递对具有很多行为的类的引用方面不太好。

其次,你需要在 UserManager 上使用IsInRoleAsyncmethod。是的,有 IsInRole 可用的非异步扩展方法,但我没有看到您使用了必需的命名空间。

所以我会做一个视图模型类: 公共类用户视图模型 { 公共字符串 ID { 获取;放; } 公共字符串用户名 { 获取;放; } 公共字符串电子邮件{获取;放; } 公共布尔 IsAdmin { 获取;放; } // 其他属性 }

然后调整你的控制器进行数据提取:

private UserManager<ApplicationUser> userManager;

public MyController()
{
    userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
}

public async Task<ActionResult> Index()
{
    var viewModels = db.Users.Select(u => new UserViewModel(){ Id = u.Id, Username = u.Username, Email = u.Email }).ToList();
    foreach (var userModel in viewModels)
    {
        userModel.IsAdmin = await userManager.IsInRoleAsync(userModel.Id, "Admin");
    }

    return View(viewModels);
}

然后调整您的视图以获取 List&lt;UserViewModel&gt; 并相应地显示数据 - 我将把它留给您完成。

【讨论】:

  • OK.. 我试图避免创建一个中间类,但我现在意识到在尝试这样做时我什至不再使用 MVC,因为我正在组合视图和控制器的东西。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 2022-10-16
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多