Search code examples
c#asp.netasp.net-identityrazor-pages

How do I use IdentityUserRoles?


I'm new to Razorpages and wanted to include the roles of the users in IdentityUser (which I have overriden with ApplicationUser for a custom groupname). Therefore exists a table UserRoles, but I'm not able to connect UserRoles to IdentityUser.

This would act as a tiny usermanager to set a group (e.g. department) and assign roles.

Here is what I'm trying to accomplish:

<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ApplicationUser[0].UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ApplicationUser[0].Group)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ApplicationUser[0].Role)
        </th>
        <th></th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model.ApplicationUser)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Group.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Role.Name)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
    }
</tbody>

Any help is appreciated - thanks a lot!


Solution

  • In my ASP.NET 5.0 Blazor Server project, with EF Core, I followed the guidelines in Identity Model Customisation to initially create as custom ApplicationUser class, which enhirits from IdentityUser (I'm sure you did the same here). In my case I also change the PK type to GUID.

    public class ApplicationUser : IdentityUser<Guid>
        {
            public virtual ICollection<IdentityUserClaim<Guid>> Claims { get; set; }
            public virtual ICollection<IdentityUserLogin<Guid>> Logins { get; set; }
            public virtual ICollection<IdentityUserToken<Guid>> Tokens { get; set; }
            public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
    

    As with IdentityUser, I also had to create custom classes for IdentityRole:

    public class ApplicationRole : IdentityRole<Guid>
        {
            public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
    

    ...as well as IdentityUserRole classes:

     public class ApplicationUserRole : IdentityUserRole<Guid>
        {
            public virtual ApplicationUser User { get; set; }
            public virtual ApplicationRole Role { get; set; }
        }
    

    And then update my ApplicationDBContext to reference the custom classes:

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

    Finally, I had to register the custom database context class when adding the identity service (in my Blazor project, under startup.cs).

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

    This would solve your issue:

    I'm not able to connect UserRoles to IdentityUser.:

    I am now able to handle relationships between users and their roles as I handle relationships with any of my other models (while also making use of UserManager and RoleManager). For example, to assign a user to a role with UserManager: await UserManager.AddToRoleAsync(ApplicationUserObject, "admin");