Add Custom User Data To Identity Core Register Form In ASP.NET Core 3.0

Today in this article I will explain how to add custom user data in your registration form with the help of Identity Core in ASP.NET Core. This article is a continuation of pervious articles. Those article links are given below.
 
Here is the complete code,
Step 1
 
Start-up Visual Studio 2019. Now click on create new project and Choose ASP.NET Core Web Application and click on “Next
 
After clicking next, another wizard will open. Under the project name, give a meaningful name to your project and click on create.
 
That will open up another new wizard; select ASP.Net Core 3.0 from the dropdown. If not, select default. Choose Web Application (Model-View-Controller) template and click on create which will create your first ASP.Net Core Application.
 
Step 2
 
Now under Models folder create class ApplicationUser. This class will inherit from IdentityUser class. Now define your fields which you want to “Add” in your registration form.
  1. using Microsoft.AspNetCore.Identity;  
  2.   
  3. namespace CustomUserData_Demo.Models  
  4. {  
  5.     public class ApplicationUser : IdentityUser  
  6.     {  
  7.         public string FirstName { getset; }  
  8.         public string LastName { getset; }  
  9.     }  
  10. }  
Step 3
 
Now open ApplicationDbContext.cs class which location under Data folder. Modify class as below,
  1. using CustomUserData_Demo.Models;  
  2. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;  
  3. using Microsoft.EntityFrameworkCore;  
  4.   
  5. namespace CustomUserData_Demo.Data  
  6. {  
  7.     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>  
  8.     {  
  9.         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
  10.             : base(options)  
  11.         {  
  12.         }  
  13.     }  
  14. }  
Step 4
 
Now open startup.cs class and “Add” following code or configure ApplicationUser class.
  1. public void ConfigureServices(IServiceCollection services)  
  2.         {  
  3.             services.AddDbContext<ApplicationDbContext>(options =>  
  4.                 options.UseSqlServer(  
  5.                     Configuration.GetConnectionString("DefaultConnection")));  
  6.   
  7.             services.AddIdentity<ApplicationUser,IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)  
  8.                 .AddEntityFrameworkStores<ApplicationDbContext>();  
  9.   
  10.             services.AddControllersWithViews();  
  11.             services.AddRazorPages();  
  12.         }  
Step 5 
 
Now create a folder called ViewModel and “Add” a class with name RegisterViewModel. We will use this as a model for register.
  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace CustomUserData_Demo.ViewModel  
  4. {  
  5.     public class RegisterViewModel  
  6.     {  
  7.         [Required]  
  8.         [Display(Name = "First Name")]  
  9.         public string FirstName { getset; }  
  10.   
  11.         [Required]  
  12.         [Display(Name = "First Name")]  
  13.         public string LastName { getset; }  
  14.   
  15.         [Required]  
  16.         [Phone]  
  17.         [Display(Name ="Phone Number")]  
  18.         public string PhoneNumber { getset; }  
  19.   
  20.         [Required]  
  21.         [EmailAddress]  
  22.         public string Email { getset; }  
  23.   
  24.         [Required]  
  25.         [DataType(DataType.Password)]  
  26.         public string Password { getset; }  
  27.   
  28.         [Required]  
  29.         [Display(Name = "Confirm Password")]  
  30.         [DataType(DataType.Password)]  
  31.         [Compare("Password", ErrorMessage = "Your password and confirm password do not match")]  
  32.         public string ConfirmPassword { getset; }  
  33.     }  
  34. }  
Step 6 
 
Now under the project, create a folder with the name Controllers. After creating the “Controllers” folder, right-click on the “Controllers” folder and add a controller.
 
A window will appear. Choose MVC Controller-Empty and click "Add".
 
After clicking on "Add", another window will appear with DefaultController. Change the name to AccountController and click on "Add". The AccountController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Account.
 
Step 7 
 
Now “Add” IActionResult method with name Register. Write the following below code to register user.
  1. using System.Threading.Tasks;  
  2. using CustomUserData_Demo.Models;  
  3. using CustomUserData_Demo.ViewModel;  
  4. using Microsoft.AspNetCore.Identity;  
  5. using Microsoft.AspNetCore.Mvc;  
  6.   
  7. namespace CustomUserData_Demo.Controllers  
  8. {  
  9.     public class AccountController : Controller  
  10.     {  
  11.         private readonly UserManager<ApplicationUser> userManager;  
  12.         private readonly SignInManager<ApplicationUser> signInManager;  
  13.         public AccountController(UserManager<ApplicationUser> userManager,  
  14.             SignInManager<ApplicationUser> signInManager)  
  15.         {  
  16.             this.userManager = userManager;  
  17.             this.signInManager = signInManager;  
  18.         }  
  19.         public IActionResult Regiter()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         [HttpPost]  
  25.         [ValidateAntiForgeryToken]  
  26.         public async Task<IActionResult> Register(RegisterViewModel model)  
  27.         {  
  28.             if (ModelState.IsValid)  
  29.             {  
  30.                 var user = new ApplicationUser  
  31.                 {  
  32.                     FirstName = model.FirstName,  
  33.                     LastName = model.LastName,  
  34.                     PhoneNumber = model.PhoneNumber,  
  35.       UserName=model.Email,  
  36.                     Email = model.Email,  
  37.                 };  
  38.                 var result = await userManager.CreateAsync(user, model.Password);  
  39.                 if (result.Succeeded)  
  40.                 {  
  41.                     await signInManager.SignInAsync(user, isPersistent: false);  
  42.                     return RedirectToAction("Index""Home");  
  43.                 }  
  44.   
  45.                 foreach (var error in result.Errors)  
  46.                 {  
  47.                     ModelState.AddModelError("", error.Description);  
  48.                 }  
  49.             }  
  50.             return View();  
  51.         }  
  52.     }  
  53. }  
Step 8 
 
Now right click on register action method and “Add View” with default name Register click on “Add”. It will be added under views folder; account folder with Register.cshtml.
  1. @model CustomUserData_Demo.ViewModel.RegisterViewModel  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Register";  
  5. }  
  6.   
  7. <div class="row">  
  8.     <div class="col-md-8 mx-auto">  
  9.         <div class="card">  
  10.             <div class="card-header bg-primary text-white">  
  11.                 <h5 class="text-uppercase text-center">User Regirtation Form</h5>  
  12.             </div>  
  13.             <div class="card-body">  
  14.                 <form asp-action="Register">  
  15.                     <div class="row">  
  16.                         <div class="col-md-6">  
  17.                             <div class="form-group">  
  18.                                 <label asp-for="FirstName"></label>  
  19.                                 <input asp-for="FirstName" class="form-control" />  
  20.                                 <span asp-validation-for="FirstName" class="text-danger"></span>  
  21.                             </div>  
  22.                         </div>  
  23.                         <div class="col-md-6">  
  24.                             <div class="form-group">  
  25.                                 <label asp-for="LastName"></label>  
  26.                                 <input asp-for="LastName" class="form-control" />  
  27.                                 <span asp-validation-for="LastName" class="text-danger"></span>  
  28.                             </div>  
  29.                         </div>  
  30.                     </div>  
  31.                     <div class="row">  
  32.                         <div class="col-md-6">  
  33.                             <div class="form-group">  
  34.                                 <label asp-for="PhoneNumber"></label>  
  35.                                 <input asp-for="PhoneNumber" class="form-control" />  
  36.                                 <span asp-validation-for="PhoneNumber" class="text-danger"></span>  
  37.                             </div>  
  38.                         </div>  
  39.                         <div class="col-md-6">  
  40.                             <div class="form-group">  
  41.                                 <label asp-for="Email"></label>  
  42.                                 <input asp-for="Email" class="form-control" />  
  43.                                 <span asp-validation-for="Email" class="text-danger"></span>  
  44.                             </div>  
  45.                         </div>  
  46.                     </div>  
  47.                     <div class="row">  
  48.                         <div class="col-md-6">  
  49.                             <div class="form-group">  
  50.                                 <label asp-for="Password"></label>  
  51.                                 <input asp-for="Password" class="form-control" />  
  52.                                 <span asp-validation-for="Password" class="text-danger"></span>  
  53.                             </div>  
  54.                         </div>  
  55.                         <div class="col-md-6">  
  56.                             <div class="form-group">  
  57.                                 <label asp-for="ConfirmPassword"></label>  
  58.                                 <input asp-for="ConfirmPassword" class="form-control" />  
  59.                                 <span asp-validation-for="ConfirmPassword" class="text-danger"></span>  
  60.                             </div>  
  61.                         </div>  
  62.                     </div>  
  63.                     <input type="submit" value="Register" class="btn btn-sm btn-primary text-uppercase rounded-0" />  
  64.                 </form>  
  65.             </div>  
  66.         </div>  
  67.     </div>  
  68. </div>  
Step 9
 
Now under views folder find shared folder and open _LoginPartial.cshtml modify it as below.
  1. @using Microsoft.AspNetCore.Identity  
  2. @inject SignInManager<ApplicationUser> SignInManager  
  3. @inject UserManager<ApplicationUser> UserManager  
  4.   
  5.     <ul class="navbar-nav">  
  6.         @if (SignInManager.IsSignedIn(User))  
  7.         {  
  8.   
  9.         }  
  10.         else  
  11.         {  
  12.             <li class="nav-item">  
  13.                 <a class="nav-link" asp-controller="Account" asp-action="Register">Register</a>  
  14.             </li>  
  15.             <li class="nav-item">  
  16.                 <a class="nav-link text-dark"  asp-controller="Account" asp-action="Login">Login</a>  
  17.             </li>  
  18.         }  
  19.     </ul>  
Step 10
 
Now press ctrl+F5 and run your project.
 
Add Custom User Data To Identity Core In Register Form In ASP.NET CORE 3.0