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

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.

Modify the Context class. Inherit it from the IdentityDbContext<ApplicationUser>.

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

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

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

Next task is 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/PagesAccounts/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

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

Finally, Open the _LoginPartial.cshtml

Remove the following code

And add this

That’s It.

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

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

  1. Unfortunately this well explained tutorial crashes with the following error :

    InvalidOperationException: Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ while attempting to activate ‘ASPNetCoreIdentityCustomFields.Areas.Identity.Pages.Account.LoginModel’.
    Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

    Seems to be happening after inserting data into the database

    1. TekTutorialsHub

      Sorry and thanks for noticing it

      You need to change IdentityUser to ApplicationUser wherever you find it.

      You will find it in the declaration of SignInManager & UserManager of Register.chtml.cs and in class constructor

      Updated the code

Leave a Comment

Your email address will not be published.

Scroll to Top