Extend IdentityUser in ASP NET Core



Extend IdentityUser in ASP NET Core

Extend IdentityUser in ASP NET Core

How to extend the built-in IdentityUser class in ASP.NET Core.

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Text version of the video
https://csharp-video-tutorials.blogspot.com/2019/06/extend-identityuser-in-aspnet-core.html

Slides
https://csharp-video-tutorials.blogspot.com/2019/06/extend-identityuser-in-aspnet-core_26.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

Why should we extend IdentityUser class

The built-in IdentityUser class has very limited set of properties like Id, Username, Email, PasswordHash etc.

What if I want to store additional data about the user like Gender, City, Country etc. The built-in IdentityUser class does not have these properties. To store custom user data like Gender, City, Country etc, extend the IdentityUser class.

Extend IdentityUser Class

You can name the class that extends the IdentityUser class anything you want, but it is customary to name it ApplicationUser. In the example below, ApplicationUser class extends the IdentityUser class. We have included just one custom property City, but you can include as many properties as you want.

public class ApplicationUser : IdentityUser
{
public string City { get; set; }
}

Find all references of IdentityUser class and replace it with our custom ApplicationUser class.

Specify ApplicationUser class as the generic argument for the IdentityDbContext class

This is how the IdentityDbContext class knows it has to work with our custom user class (in this case ApplicationUser class) instead of the default built-in IdentityUser class.

public class AppDbContext : IdentityDbContext[ApplicationUser]
{
public AppDbContext(DbContextOptions[AppDbContext] options)
: base(options)
{
}

public DbSet[Employee] Employees { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Seed();
}
}

Generate a new migration to add columns to AspNetUsers table

Add-Migration Extend_IdentityUser

Next, apply the migration to the database using the following command

Update-Database

Extending IdentityUser – Add-Migration Not Working

If ApplicationUser class (the class that extends IdentityUser class) is not specified as the generic argument for IdentityDbContext class, Add-Migration command does not work. It does not generate the required migration code to add the adiitional columns to the AspNetUsers identity table.

To fix this, specify ApplicationUser class as the generic argument for IdentityDbContext class.

If you get the following exception, the most likely cause is somewhere with in your application you are still using IdentityUser class instead of the ApplicationUser class.
No service for type ‘Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ has been registered.

In Visual Studio, search for IdentityUser class throughout your application using CTRL + SHIFT + F

Replace IdentityUser class with ApplicationUser class and rerun the project.

Storing custom data in AspNetUsers table

Comments are closed.