In this article, we will learn how to list all users with Associated Roles in ASP.NET MVC 5 using Identity. ASP.NET MVC 5 does not come with an inbuilt feature to list users with associated roles by default. However ASP.NET MVC have inbuilt UserManager, SignManager and RoleManager to assist this.
We need this feature in each of our applications as users are to be maintained along with their associated roles. We can apply a number 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
- 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 2Add a new method called UsersWithRoles inside ManageUsersController and add the following codes.
Code Snippet
- 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);
- }
Note - context.Users represents the table AspNetUsers which has navigation property roles which represent the AspNetUserInRoles table.
Step 3
Now, let’s add a View of UsersWithRoles method of ManageUsersController.
- @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="UsersWithRoles">
- <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 () {
- $('#UsersWithRoles').DataTable({
- "paging": true,
- "lengthChange": true,
- "searching": true,
- "ordering": true,
- "info": true,
- "autoWidth": true
- });
- });
- </script>
- }
Result
Now, the above method and View will return the users to their roles, as shown in the following figure.
View returning list of users with their associated Roles in ASP-NET MVC.
Note
You need to have the list of users associated with each role in the database.
Source Code
https://github.com/nishanaryal/ASP.NET-MVC