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
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.
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.
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.
IdentityDbContext() Initializes a new instance of the IdentityDbContext class. IdentityDbContext(DbContextOptions) Initializes a new instance of IdentityDbContext.
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)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With