10389total visits.
In this article, we will leanr how to list all users with Associated ROles in ASP.NET MVC 5 using Identity. ASP.NET MVC 5 does not have an inbuilt feature to list users with associated roles by default. However ASP.NET MVC have inbuilt UserManager, SignManager and RoleManager to assist this, but yet there is no prebuilt method that can list users associated with Users.
We need this feature in each of our Application as users are to be maintained along with their associated Roles. We can apply no of ideas to do this. In this article, we will learn a very simple way to list Users with their associated RoleName as in the figure below.
Step 1: Create a View Model as Users-in-Role_ViewModel

Code Snippet :
|
1 2 3 4 5 6 7 |
public class Users_in_Role_ViewModel { public string UserId { get; set; } public string Username { get; set; } public string Email { get; set; } public string Role { get; set; } } |
Step 2: Add New Method called UsersWithRoles inside ManageUsersController and add following codes.

Code Snippet :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public ActionResult UsersWithRoles() { var usersWithRoles = (from user in context.Users select new { UserId = user.Id, Username = user.UserName, Email = user.Email, RoleNames = (from userRole in user.Roles join role in context.Roles on userRole.RoleId equals role.Id select role.Name).ToList() }).ToList().Select(p => new Users_in_Role_ViewModel() { UserId = p.UserId, Username = p.Username, Email = p.Email, Role = string.Join(",", p.RoleNames) }); return View(usersWithRoles); } |
context.Users represent the table AspNetUsers which has a navigation property Roles which represents the AspNetUserInRoles table. Then we perform join of context.Roles which represents the AspNetRoles table to get the role name.
Step 3: Now let’s add a view of UsersWithRoles method of ManageUsersController as
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
@model IEnumerable<MVC5Demo.UI.ViewModels.Users_in_Role_ViewModel> @{ ViewBag.Title = "Users With Roles"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="box-title"> <b>Users with Roles</b> </h3> </div> <!-- /.box-header --> <div class="panel-body"> <table class="table table-hover table-bordered table-condensed" id="example1"> <thead> <tr> <td><b>Username</b></td> <td><b>Email</b></td> <td><b>Roles</b></td> </tr> </thead> @foreach (var user in Model) { <tr> <td>@user.Username</td> <td>@user.Email</td> <td>@user.Role</td> </tr> } </table> </div> <div class="panel-footer"> <p class="box-title"><b>Total Users till @String.Format("{0 : dddd, MMMM d, yyyy}", DateTime.Now) : </b><span class="label label-primary">@Model.Count()</span></p> </div> </div> @section scripts{ <script> $(function () { $("#example1").DataTable(); $('#example2').DataTable({ "paging": true, "lengthChange": true, "searching": true, "ordering": true, "info": true, "autoWidth": true }); }); </script> } |
Result:
Now the above method and view will return Users with their roles as shown in the following figure.

Note: You need to have the list of users associated with each role.
Source Code : https://github.com/nishanaryal/ASP.NET-MVC