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).
Here's my context:
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");
My index action in UsersController.cs:
public ActionResult Index()
{
return View(db.Users.Include(i => i.Roles).ToList());
}
Index view:
@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>
My Roles table:
My UserRoles table:
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")?