ASP.NET Core 2.1 displaying users and roles

ASP.NET Core 2.1 displaying users and roles

Trying to display users along with their role in core 2.1, I followed the example&nbsp;<a href="https://stackoverflow.com/questions/51004516/net-core-2-1-identity-get-all-users-with-their-associated-roles" target="_blank">stackoverflow</a>&nbsp;. I have no errors but I'm not getting user roles to display. I think it could be my view, for some reason my code returns an email address instead of user role.

Trying to display users along with their role in core 2.1, I followed the example stackoverflow . I have no errors but I'm not getting user roles to display. I think it could be my view, for some reason my code returns an email address instead of user role.

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, 
ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>

{ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity&lt;ApplicationUserRole&gt;(userRole =&gt;
    {
        userRole.HasKey(ur =&gt; new { ur.UserId, ur.RoleId });

        userRole.HasOne(ur =&gt; ur.Role)
            .WithMany(r =&gt; r.UserRoles)
            .HasForeignKey(ur =&gt; ur.RoleId)
            .IsRequired();

        userRole.HasOne(ur =&gt; ur.User)
            .WithMany(r =&gt; r.UserRoles)
            .HasForeignKey(ur =&gt; ur.UserId)
            .IsRequired();
    });
}

public DbSet&lt;ApplicationUser&gt; ApplicationUser { get; set; }

}

Startup.cs

services.AddIdentity<ApplicationUser, ApplicationRole>()
      .AddEntityFrameworkStores<ApplicationDbContext>()
      .AddDefaultUI()
      .AddDefaultTokenProviders();

ApplicationUser

public class ApplicationUser : IdentityUser
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }

public class ApplicationUserRole : IdentityUserRole&lt;string&gt;
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}
public class ApplicationRole : IdentityRole
{
    public ICollection&lt;ApplicationUserRole&gt; UserRoles { get; set; }
}

}

Index method:

public async Task<IActionResult> Index(int? page )
{
   var users = from u in _userManager.Users.Include(u => u.UserRoles).ThenInclude(ur => ur.Role) select u;

return View(users.ToPagedList(pageNumber, pageSize)); }

View

@Html.DisplayFor(m => item.UserRoles)


asp.net

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Routing in MVC - ASP.NET Core Demystified

ASP.NET Core MVC has introduced quite a few concepts that new (or new-to-ASP.NET) web developers might have some difficulty getting caught up with. My ASP.NET Core Demystified series is designed to help these developers get started building their own custom, full-fledged, working AASP.NET Core applications. In

Add asp.net Identity in empty project

LIKE | COMMENT | SHARE | SUBSCRIBE Today, in this tutorial you will see the process of adding an ASP.NET Identity to the ASP.NET Web Application developed in...

List all users from asp net core identity

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, i will show you How to retrieve and display all the registered application users in asp.net core using the ...

Extend IdentityUser in ASP NET Core

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorials, I will show you how to extend IdentityUser in asp.net core.. Subscribe & Stay Connected! Thank You! ♥ #...

Adding an endpoint graph to your ASP.NET Core application: Visualizing ASP.NET

In this post I show how the endpoint routes in an ASP.NET Core 3.0 application can be visualized as a directed graph, and explore the information it