Today I will explain how to enable identity core in ASP.NET MVC core application. This article will help you to understand the steps for enabling identity and working with security in asp.net core.
Here is the complete code.
What is ASP.NET Core Identity?
ASP.NET Core Identity is a membership system. It allows us to create, read, update, and delete user accounts. It supports account confirmation, authentication, authorization, password recovery, and two-factor authentication with SMS. It also supports external login providers like Microsoft, Facebook, Google, etc.
Step 1. Startup Visual Studio 2019. Now click on Create New Project 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 an empty template and click on create which will create your first ASP.Net Core Application.

Step 2. Click on tools, select Nuget Package Manager, then click on manage Nuget Package for solution. Now click on the browse tab then search for the following packages and install them.
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Relational
- Microsoft.EntityFrameworkCore.Tools
- Microsoft.AspNetCore.Identity.EntityFrameworkCore

Step 3. Now create a Models folder under your project and create ApplicationDbContext, Employee, and Department class. Now inherit the ApplicationDbContext class from IdentityDbContext.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace EnableIdentityDemo.Models
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
namespace EnableIdentityDemo.Models
{
public class Employee
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter first name")]
[Display(Name = "First name")]
[StringLength(100)]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter last name")]
[Display(Name = "Last name")]
[StringLength(100)]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter age")]
public int Age { get; set; }
[Required(ErrorMessage = "Please enter position")]
[StringLength(100)]
public string Position { get; set; }
[Required(ErrorMessage = "Please enter office")]
[StringLength(100)]
public string Office { get; set; }
[Required(ErrorMessage = "Please enter salary")]
public int Salary { get; set; }
[Required(ErrorMessage = "Please choose department name")]
[Display(Name = "Department")]
public int DepartmentId { get; set; }
public Department Department { get; set; }
}
}




Dipak BarnwalPosted Dec 25, 2019, 6:51 AM
Nice article thanks for sharing.
Rushi MehtaPosted Dec 14, 2019, 3:20 AM
Thanks for sharing
Prabakaran MPosted Dec 12, 2019, 11:18 PM
Nice Article Thanks For Sharing
Sourav Kumar DasPosted Dec 11, 2019, 11:07 PM
Nice useful article. Thanks for sharing.
Chittaranjan SwainPosted Dec 11, 2019, 11:03 PM
Nice article.