Add Custom Fields To User in ASP.NET Core Identity

In this tutorial, we will show you how to add custom fields to the user table in ASP.NET Core identity. We do that by creating a custom user table by extending the IdentityUser entity. Also, we will show you how to add these fields to the Registration form.

Create a new Identity Project

Create a new project in Visual Studio 2019. Select the template ASP.NET Core Web App. under the Authentication Type select Individual Accounts. Select Target Framework as .NET 5.0. Name the project as ASPNetCoreIdentityCustomFields

Run the app to ensure that everything is ok.

Update the Connection String to use the SQL Server from appsettings.json.

Run the migrations to create the database

Check the App to see if everything is ok.

Now, open the SQL Server and explorer the tables. One of the tables is AspNetUsers table. This table represents the IdentityUser Entity

AspNetUsers Table

Add Custom Fields to User Entity

To Add Custom fields, we need to create a new User Entity class extending it from the IdentityUser Class. We will then add the custom fields to that class.

Create a new ApplicationUser class under the folder Data. We have added two fields firstName & lastName to the Entity

Data/ApplicationUser.cs

Note that our ApplicationUser class extends IdentityUser class.

Configure Application to use the new User Model

We want our application to use the newly created ApplicationUser class instead of the IdentityUser class.

Open the ApplicationDbContext class from the data folder and modify it to inherit from IdentityDbContext<ApplicationUser>.

Update the AddDefaultIdentity in the startup class to use the ApplicationUser instead of identityUser

Finally, Open the _LoginPartial.cshtml

Remove the following code

And add this

Configure the model & Update database

We also use the ModelBuilder to configure the field lengths of FirstName & LastName Columns. Remember to call the OnModelCreating method of the base class.

Finally, add the migration and update the database.

Check the database. You will find the new fields firstName & LastName in the AspNetUsers table.

Scaffold UI Forms

The next task is to update the Register Form.

The Identity API hides the UI forms from us in the Microsoft.AspNetCore.Identity.UI namespace. But we can Scaffold it and modify it.

Follow these steps the Scaffold the Identity.

Right-click on the Solutions folder Add -> New Scaffolded Item

Scaffold Identity

Select Identity

Add New Scaffold Identity Item
  1. Ensure that the correct Layout is chosen
  2. Select Account/Register
  3. From the dropdown select the context class i.e. ApplicationDbContext
  4. Click on Add
Add Identity for Custom Fields in IdentityUser

Register Page

Now Open the Areas/Identity/Pages/Accounts/Register.cshtml

Change IdentityUser to ApplicationUser wherever you find it. You will find in the declaration of SignInManager & UserManager.

Include in the InputModel

You also need to import the namespace

And change this in the OnPostAsync method

Open the Register.cshtml and add this HTML after the Email (or before)

That’s It.

Now, run the app and test the user registration form. Also, check if the fields are correctly updated.

11 thoughts on “Add Custom Fields To User in ASP.NET Core Identity”

  1. Richard Robson, Sr

    This broke my mind. I am in a production scenario where I am moving from an older identity approach (WebPages Authentication/Authorization) to the latest Identity solution and the identity tables in the database are NEW but the tens of business tables are legacy and I must use Database first – i.e., I do not want to go into a huge data conversion effort because I changed Identity approaches.

  2. This worked well for me on .Net 7, but I did have to change IdentityUser to ApplicationUser in the other profile management pages by overriding those as well using the scaffolder (https://andrewlock.net/customising-aspnetcore-identity-without-editing-the-pagemodel). Only changing it on the login and registration pages leaves a lot of the other profile management pages broken, including password reset, profile editing, etc. Fixing all those things was easy by doing the scaffolded overrides, but it was still a lot that wasn’t represented here.

  3. Thank you, this was by far the clearest explanation of how to extend the IdentityUser class and underlying database! I looked at many other articles and explanations – none of which made sense nor worked!

  4. you need to include your reference to ASPNetCoreIdentityCustomFields.Data in the _LoginPartial page…

    @using ASPNetCoreIdentityCustomFields.Data;

  5. This is great! i’ve already done it but it took from me more time than i was expecting. If i saw this earlier i would have done it faster

  6. You may also need to update the dependency injection code on the _ManageNav.cshtml file inder .AreasPagesAccountManage folder AND add a reference to your ApplicationObject in _ViewImports in the same folder.

    Add “@using Survey.Data” to _ViewImports.cshtml and

    change

    @inject SignInManager SignInManager

    to

    @inject SignInManager SignInManager

  7. Going along great until I got to this section … “Configure the model & Update database”, where exactly does this code go?

    1. He’s using the .NET Entity Framework to handle his database work. He assumes you are doing the same. Look up “code first database design” and “.NET Entity Framework” and learn how to use database migrations.

  8. I am getting an error “There is already an object named ‘AspNetRoles’ in the database” after I call the command update-database

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top