Tutorial: ASP.NET Core with Identity

Alpesh Patel
.Net Programming
Published in
4 min readApr 3, 2021

In this tutorial, we will discuss about identity authentication implementation on ASP.Net Core. ASP.NET Core Identity framework is used to implement forms authentication. There are many options to choose from for identifying your users including Windows Authentication and all of the third-party identity providers like Google, Facebook and Microsoft etc.

ASP.NET Core Identity is an API that supports user interface (UI) login functionality and manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

Identity is typically configured using a SQL Server database to store user names, passwords, and profile data. Alternatively, another persistent store can be used, for example, Azure Table Storage.

Here is the source code of the entire implementation.

Prerequisites

The following must be installed in your system:

Step 1 New Project

Once you have installed the Visual Studio 2019 and .Net Core 3.1 SDK, you can start building a new ASP.NET Core Application with authentication.

  • Open VS2019
  • On the start page, choose Create a new project.
  • Create a web app and From the Visual Studio select Create a new project. Select ASP.NET Core Web Application > Next.
  • Give your project a name i.e. ASPDotNetCoreWithIdentity and give your project a location where it’ll be saved and click Create.
  • Now, you need to choose the framework version and MVC project Template (Web Application), click on change button on Authentication and select Individual User Accounts:
  • After selection of Authentication click on Create button as below:
  • After the project created successfully, you can see all dependency NuGet Packages already installed and created new folder structure for Identity:
  • Go to Startup.cs and see already added identity services as below:

Step 2: Create Identity Database using Migration:

  • Now, you need to configure you database connection and it will create database for identity using core first approach. As below open “appsettings.json” and edit you connection string.
  • You can see on Data folder already migrations is created by default for Identity as below:
  • Apply the migrations to initialize the database run update-database command on Package Manager Console as below:
  • We just need to go to SQL Server Management Studio and login and see the Database. You will find the database which name mention on connection string. You can see full identity full database is created with all tables as below:
  • Let’s add some more services on startup.cs in ConfigureServices. The typical pattern is to call all the Add{Service} methods.

Step 3: Run Project

Now, let’s run project using ctrl+f5:

  • Now, you click on register and login button on header and you can see register and login page is already there without any written coding.

Step 4: Add Identity using Scaffold

  • You have readymade pages and code for default features of authentication so no need to write any code for default features of authentication for ex. Login, Register, Forgot Password, Reset Password, Conformation, etc.
  • Let’s add all pages using Scaffold and you can view and modify if needed:
  • After click on Add button, open add identity dialog box and you can see all features on this box and you can select, add and customized feature.
  • Let’s select basic feature and add.
  • Now, you can see this all in your Area -> Identity -> Pages. You can change code based on your requirement.

Thank you…!

--

--

Alpesh Patel
.Net Programming

Technology Specialist and Full-Stack Developer. Find me on linkedin for more details https://www.linkedin.com/in/alpesh-patel-8b930952