Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom column to AspNetUserRoles table in ASP.NET Core 3.0 - ASP.NET Core IDENTITY

I created the tables through migrations to manage the security of my ASP.NET Core project using ASP.NET Core identity.

ASP.NET Core identity tables

I would need to be able to insert an additional field in the AspNetUserRoles table in order to manage the users already associated with the ADMIN role in a different way.

For example I have ADMINs associated with a USA nation and ADMINs associated with another BRAZIL or ADMIN nation associated with ITALY, but their real role always remains ADMIN.

In practice it is just one more attribute to add when I associate a user with the ADMIN role.

ASP.NET Core identity tables with custom column in AspNetUserRoles table

I tried to extend the IdentityUserRole class in this way:

public class AspNetUserRoles : IdentityUserRole<string>
{
    public string Nation { get; set; }
}

but then when I launch the migration script (code-first) in the migration classes no changes are detected!

PM> Add-Migration aspnetuserrolesupdate -Context ApplicationDbContext

this is the result:

public partial class aspnetuserrolesupdate : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}

As you can see from the product code no changes were found ....

In my project I have already inserted fields in the AspNetUsers and AspNetRoles tables successfully in this way, but how can I add a third column to the AspNetUserRoles table?

Thank you all

like image 772
gemon01 Avatar asked Mar 31 '20 14:03

gemon01


People also ask

How do I add a field to the default AspNetUser table?

To add additional columns to ASP.Net Core Identity, we need to create a model inheriting the IdentityUser properties. This method will allow us to specify an additional field for IdentityUser. To do so, make a model class name ApplicationUser.

How do I access the AspNetUserRoles table?

AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user. AspNetRoles; for each of the users.

How does core identity work in asp net?

ASP.NET Core Identity provides a framework for managing and storing user accounts in ASP.NET Core apps. Identity is added to your project when Individual User Accounts is selected as the authentication mechanism. By default, Identity makes use of an Entity Framework (EF) Core data model.

What is IdentityDbContext?

IdentityDbContext() Initializes a new instance of the IdentityDbContext class. IdentityDbContext(DbContextOptions) Initializes a new instance of IdentityDbContext.


1 Answers

I solved it like this:

I modified the injected classes, adding them all to have ApplicationUserRole available

public class ApplicationDbContext: // IdentityDbContext <ApplicationUser, ApplicationRole, string>
     IdentityDbContext <
     ApplicationUser, ApplicationRole, string,
     IdentityUserClaim <string>, ApplicationUserRole, IdentityUserLogin <string>,
     IdentityRoleClaim <string>, IdentityUserToken <string>>
{
     public ApplicationDbContext (DbContextOptions <ApplicationDbContext> options)
         : base (options)
     {
     }
}
like image 54
gemon01 Avatar answered Oct 04 '22 22:10

gemon01