查看用户角色显示角色ID而不是角色名称 [英] Viewing user roles show the role id instead of the role name

查看:116
本文介绍了查看用户角色显示角色ID而不是角色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.NET Identity中配置基于角色的授权.我要这样做,以便管理员用户可以看到用户及其角色的列表(在索引"页面上),并可以更改单个用户的角色(在编辑"页面上).

I'm trying to configure role-based authorization in ASP.NET Identity. I want to make it so that Admin users can see a list of users and their roles (on the Index page) and change the roles of individual users (on an Edit page).

这是我的上下文:

modelBuilder.Entity<IdentityUser>()
    .HasKey<string>(k => k.Id)
    .ToTable("Users");
modelBuilder.Entity<IdentityUserRole>()
    .HasKey(r => new { r.RoleId, r.UserId })
    .ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin>()
    .HasKey<string>(f => f.UserId)
    .ToTable("UserLogins");
modelBuilder.Entity<IdentityRole>()
    .ToTable("Roles");

我在UsersController.cs中的索引操作:

My index action in UsersController.cs:

public ActionResult Index()
    {
        return View(db.Users.Include(i => i.Roles).ToList());
    }

索引视图:

@model List<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>

//stuff

    @foreach (var item in Model)
    {
        <tr class="index-item" data-href="@Url.Action("Details", "Users", new { id=item.Id })">
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Roles)">
            </td>
            <td class="index-links">
                //stuff
            </td>
        </tr>
    }
</table>

我的角色表:

我的UserRoles表:

My UserRoles table:

基于这些表,用户具有查看者"角色.但是,该视图未显示角色名称,而是显示了角色ID:

Based on these tables, the user has the "Viewer" role. However, instead of showing the Role name, the view shows the Role ID:

如何使它显示角色名称(在本例中为查看器")?

How do I make it show the Role name instead (in this case, "Viewer")?

推荐答案

IdentityUserRole类没有IdentityUserIdentityRole的导航属性,只有UserIdRoleId,它不那么容易获得用户的角色.所以我们需要使用2个db调用

IdentityUserRole class have no navigation properties for IdentityUser and IdentityRole, just UserId and RoleId, its not that easy to get the roles of a user. So we need to use 2 db calls

var users = db.Users.Include(u => u.Roles);
var roles = db.Roles.Include(r => r.Users);

像这样的ViewModel

A ViewModel like this

public class UsersRolesViewModel 
{
     public List<ApplicationUser> Users { set; get; }
     public List<IdentityRole> Roles { set; get; }
}

和视图中的

@foreach (var user in Model.Users)
{
    <tr class="index-item" data-href="@Url.Action("Details", "Users", new { id= user.Id })">
        <td>
            @user.Email
        </td>
        <td>
            @string.Join(",", roles.Where(r => r.Users.Any(u => u.UserId == user.Id)).Select(r => r.Name))
        </td>
        <td class="index-links">
                //stuff
        </td>
    </tr>
}

修改

您只需一次查询就可以获取所有想要的数据

You can get all the data you want you with one query

var users = (from u in db.Users
             let query = (from ur in db.Set<IdentityUserRole>()
                          where ur.UserId.Equals(u.Id)
                          join r in db.Roles on ur.RoleId equals r.Id select r.Name)
                          select new UserDto() {User = u, Roles = query.ToList<string>()})
                         .ToList();

使用这个简单的模型

public class UserDto
{
    public ApplicationUser User { set; get; }
    public List<string> Roles { set; get; } 
}

和视图中的

  @foreach (var user in Model.Users)
  {
        var appUser = user.ApplicationUser;
        var roles = user.Roles;

        <tr class="index-item" data-href="@Url.Action("Details", "Users", new { id= appUser.Id })">
            <td>
                @appUser.Email
            </td>
            <td>
                @string.Join(",", roles)
            </td>
            <td class="index-links">
                    //stuff
            </td>
        </tr>
    }

这篇关于查看用户角色显示角色ID而不是角色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆