ASP.NET Core MVC Login and Registration using Identity

ASP.NET Core MVC Login and Registration using Identity

What is identity in ASP.NET?

ASP.NET Identity provides a robust and customizable framework for managing user authentication, authorization, and identity-related features in web applications. Some key features of ASP.NET Identity include:

  1. User Authentication:

  2. User Management:

  3. Role-Based Authorization:

  4. Claims-Based Identity:

  5. Integration withASP.NETFrameworks:

To start with the ASP.NET Identity model, Create a web app with Model, View, Controller

  1. Go to nutget package manager and install the package named Microsoft.VisualStudio.Web.CodeGeneration.Design

    It is a code generation tool for ASP.NET Core. Contains the dotnet-aspnet-codegenerator command used for generating controllers and views.

  2. Now add identity to your project

    Add a scaffold item as:

    Select Identity model and choose the desired scaffolds

    include <partial name="_LoginPartial" /> in the header section of _Layout.cshtml and finally include app.MapRazorPages(); controller in Program.cs

  3. Now run migration commands.

    1. add-migration

    2. update-database

  4. Run the application and try registration and login

    1. Now expand the user identity model to add other details like first name , last name to registration page.

Creating custom user model

  1. Creating a custom model class file

    Create a modal class file for your custom user in Areas/Identity/Data/CustomUser.cs

  2. Configure the entity mapping for a custom user entity named CustomUser. Update dbcontext file as required. The model class defines the method for Custom user model clas.

  3. The custom user model class is ready for migration. Now, reflect the changes on the database by first creating a migration file and the updating database

    1. add-migration: to create a migration file

    2. update-database: to reflect the changes on the database.

  4. Now update the register.cshtml file to include firstname and lastname for registration.

     <div class="form-floating mb-3">
         <input asp-for="Input.FirstName" class="form-control" autocomplete="FirstName" aria-required="true" placeholder="FirstName.com" />
         <label asp-for="Input.FirstName">FirstName</label>
         <span asp-validation-for="Input.FirstName" class="text-danger"></span>
     </div>
     <div class="form-floating mb-3">
         <input asp-for="Input.LastName" class="form-control" autocomplete="LastName" aria-required="true" placeholder="LastName.com" />
         <label asp-for="Input.LastName">LastName</label>
         <span asp-validation-for="Input.LastName" class="text-danger"></span>
     </div>
    
  5. Register.cshtml.cs must be updated with fields to be added on the Register.cshtml so include first name and last name as:

       public class InputModel
       {
    
           [Required]
           [Display(Name = "First Name")]
           public string FirstName { get; set; }
    
           [Required]
           [Display(Name = "Last Name")]
           public string LastName { get; set; }
    
     ................
    
  6. Build the application to get new registration page

Did you find this article valuable?

Support Sudip Bhandari by becoming a sponsor. Any amount is appreciated!