How to add custom fields in ASP.NET Identity User Registration

Background

In this Article I will explain How to add custom fields in ASP.NET Identity User Registration form. If you are working with ASP.NET Identity for user authentication in your application, default registation form comes with very few fields, i.e. Email and Password. However if you want to add custom fields and you don’t know how to do it then you will find this post helpful. You can watch Video Tutorial of this article Here on my YouTube Channel

In future articles i will explain how to extend Microsoft Identity and create custom Identity Provider as well.

User Registration Page with custom fields
User Registration Page with custom fields

Open Visual studio, and create a new ASP.NET MVC Project. select MVC Template and Add User Authentication option to Individual Account

Once your project is create from solution explorer, go to Models Folder and open IdentityModels.cs

Inside IdentityModels.cs , you will find ApplicationUser Class inherited from IdentityUser. add your custom properties inside this class.

ApplicationUser Class code given below

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        //Add Custom Fields Here
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

ASP.NET Identity uses code first approach. This Class is responsible to generate model for your User registration. At this point you can update your database by running CodeFirst Migration Commands. this will update your database. to run these commands go to Tools >> Nuget Package Manager >> Package Manager Console

Package Manager Console

In Package Manager Run following commands one by one.

1: enable-migrations 
2: Add-Migration 'custom-fields-added'
3: update-database

Code First Migrations:

Code First Migrations is the recommended way to evolve your application’s database schema if you are using the Code First workflow. Migrations provide a set of tools that allow:

  1. Create an initial database that works with your EF model
  2. Generating migrations to keep track of changes you make to your EF model
  3. Keep your database up to date with those changes

Enabling Migrations

This command adds a Migrations folder to our project. This new folder contains two files:

Add-Migration [migration-name]

Creates a migration by adding a migration snapshot.

Update-Database

This command will update your database accordingly. behind the scenes, it runs DDL commands to modify tables and add/remove colums with appropriate SQL data types. Read More