Register Form In ASP.NET MVC Core 3.0

Today in this article I will explain how to create a registration form in ASP.NET MVC core. We will look at it step by step. To understand this article  I recommend you  read my pervious articles, linked below. This article is a continuation of previous articles.
 
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
 
Register Form In ASP.NET MVC Core 3.0
 
After clicking next, another wizard will open. Under the project name, give a meaningful name to your project and click on create.
 
Register Form In ASP.NET MVC Core 3.0
 
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.
 
Register Form In ASP.NET MVC Core 3.0
Step 2
 
Click on tools, select nuget Package manager, then click on manage nuget package for solution. Now click on browse tab then search for the following package and install them.
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Relational
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
Step 3
 
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;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace RegistrationFormCore.ViewModels  
  8. {  
  9.     public class RegisterViewModel  
  10.     {  
  11.         [Required]  
  12.         [EmailAddress]  
  13.         public string Email { get; set; }  
  14.   
  15.         [Required]  
  16.         [DataType(DataType.Password)]  
  17.         public string Password { get; set; }  
  18.   
  19.         [Required]  
  20.         [Display(Name ="Confirm Password")]  
  21.         [DataType(DataType.Password)]  
  22.         [Compare("Password",ErrorMessage ="Your password and confirm password do not match")]  
  23.         public string ConfirmPassword { get; set; }  
  24.   
  25.     }  
  26. }  
Step 4
 
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.
 
Register Form In ASP.NET MVC Core 3.0
 
A window will appear. Choose MVC Controller-Empty and click "Add".
 
Register Form In ASP.NET MVC Core 3.0
 
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.
 
Register Form In ASP.NET MVC Core 3.0
 
Step 5
 
Now “Add” IActionResultmethod with name Register. Write  the following below code to register user.
 
Controller class code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Identity;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using RegistrationFormCore.ViewModels;  
  8.   
  9. namespace RegistrationFormCore.Controllers  
  10. {  
  11.     public class AccountController : Controller  
  12.     {  
  13.         private readonly UserManager<IdentityUser> userManager;  
  14.         private readonly SignInManager<IdentityUser> signInManager;  
  15.   
  16.         public AccountController(UserManager<IdentityUser>userManager,SignInManager<IdentityUser>signInManager)  
  17.         {  
  18.             this.userManager =userManager;  
  19.             this.signInManager = signInManager;  
  20.         }  
  21.   
  22.         public IActionResult Register()  
  23.         {  
  24.             return View();  
  25.         }  
  26.   
  27.         [HttpPost]  
  28.         public async Task<IActionResult> Register(RegisterViewModel model)  
  29.         {  
  30.             if (ModelState.IsValid)  
  31.             {  
  32.                 var user = new IdentityUser   
  33.                 {   
  34.                     UserName=model.Email,  
  35.                     Email=model.Email  
  36.                 };  
  37.                 var result = await userManager.CreateAsync(user, model.Password);  
  38.   
  39.                 if (result.Succeeded)  
  40.                 {  
  41.                     await signInManager.SignInAsync(user, isPersistent: false);  
  42.                     return RedirectToAction("Index""Home");  
  43.                 }  
  44.                 foreach (var error in result.Errors)  
  45.                 {  
  46.                     ModelState.AddModelError("",error.Description);  
  47.                 }  
  48.             }  
  49.             return View();  
  50.         }  
  51.     }  
  52. }  
Step 6
 
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 RegistrationFormCore.ViewModels.RegisterViewModel  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Register";  
  5. }  
  6.   
  7. <div class="row">  
  8.     <div class="col-md-6 mx-auto">  
  9.         <div class="card">  
  10.             <div class="card-header bg-danger text-white">  
  11.                 <h5 class="text-uppercase text-center">Regirtation Form</h5>  
  12.             </div>  
  13.             <div class="card-body">  
  14.                 <form asp-action="Register">  
  15.                     <div asp-validation-summary="All" class="text-danger"></div>  
  16.                     <div class="form-group">  
  17.                         <label asp-for="Email"></label>  
  18.                         <input asp-for="Email" class="form-control" />  
  19.                         <span asp-validation-for="Email" class="text-danger"></span>  
  20.                     </div>  
  21.                     <div class="form-group">  
  22.                         <label asp-for="Password"></label>  
  23.                         <input asp-for="Password" class="form-control" />  
  24.                         <span asp-validation-for="Password" class="text-danger"></span>  
  25.                     </div>  
  26.                     <div class="form-group">  
  27.                         <label asp-for="ConfirmPassword"></label>  
  28.                         <input asp-for="ConfirmPassword" class="form-control" />  
  29.                         <span asp-validation-for="ConfirmPassword" class="text-danger"></span>  
  30.                     </div>  
  31.                     <input type="submit" value="Register" class="btn btn-sm btn-success text-uppercase rounded-0" />  
  32.                 </form>  
  33.             </div>  
  34.         </div>  
  35.     </div>  
  36. </div>  
Step 7
 
Now modify _Layout.cshtm and “Add” a link with Register.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6.     <title>@ViewData["Title"] - RegistrationFormCore</title>  
  7.     <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />  
  8.     <link rel="stylesheet" href="~/css/site.css" />  
  9. </head>  
  10. <body>  
  11.     <header>  
  12.         <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">  
  13.             <div class="container">  
  14.                 <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">RegistrationFormCore</a>  
  15.                 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"  
  16.                         aria-expanded="false" aria-label="Toggle navigation">  
  17.                     <span class="navbar-toggler-icon"></span>  
  18.                 </button>  
  19.                 <div class="navbar-collapse collapse">  
  20.                     <ul class="navbar-nav flex-grow-1">  
  21.                         <li class="nav-item">  
  22.                             <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>  
  23.                         </li>  
  24.                         <li class="nav-item">  
  25.                             <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>  
  26.                         </li>  
  27.                     </ul>  
  28.                     <ul class="navbar-nav ml-auto">  
  29.                         <li class="nav-item">  
  30.                             <a class="nav-link" asp-controller="Account" asp-action="Register">Register</a>  
  31.                         </li>  
  32.                     </ul>  
  33.                 </div>  
  34.             </div>  
  35.         </nav>  
  36.     </header>  
  37.     <div class="container">  
  38.         <main role="main" class="pb-3">  
  39.             @RenderBody()  
  40.         </main>  
  41.     </div>  
  42.     <script src="~/lib/jquery/dist/jquery.min.js"></script>  
  43.     <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>  
  44.     <script src="~/js/site.js" asp-append-version="true"></script>  
  45.     @RenderSection("Scripts", required: false)  
  46. </body>  
  47. </html>  
Step 7
 
Now press ctrl+F5 and run your project.
 
Register Form In ASP.NET MVC Core 3.0