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.

Leave a Comment

Your email address will not be published.

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

Scroll to Top