.NET Core | Identity

Karim Samir
simplifycoding
Published in
4 min readMar 27, 2024

ASP.NET Core Identity is a fully featured membership system for creating and maintaining user logins. Using Identity API, you can sign in & sign out users, reset their passwords, lockout users & Implement Multi Factor Authentication. It can also integrate with the external login providers like Microsoft Account, Facebook, Google, etc.

User Manager

The UserManager is a concrete class that manages the user. This Class Creates, Updates, and Deletes the Users. It has methods to find a user-by-User ID, Username, and email. UserManager also provides the functionality for adding Claims, removing Claims, add and removing roles, etc. It also generates password hash, Validates Users etc.

Sign In Manager

SignInManager is a concrete class which handles the user sign in from the application.

The SignInManager is responsible for Authenticating a user, i. e signing in and signing out a user. It issues the authentication cookie to the user.

Practice 👨‍💻

Installing Identity

Install Identity:

Install-Package Microsoft.AspNetCore.Identity

Using EF Core with Identity:

Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore

SQL Server is Our Database:

Install-package Microsoft.EntityFrameworkCore.SqlServer

Need EF Core Tools to generate EF Core Migrations:

Install-Package Microsoft.EntityFrameworkCore.Tools

Preparing the Database: Connection String

The Database Connection strings are stored in appsettings.json file. Open it and add the connection string as follows.

Database Context

The DBContext is the class, which provides API to interact between EF Core entity models and the database. We usually create our context class inheriting from the DBContext class.

ASP.NET Core Identity API already implements IdentityDbContext class, which is inherited from the DBContext class. It already includes the DbSet Properties for the Identity Models.

Hence, we create our applications DbContext class, inheriting from the IdentityDbContext.

Go to the Project folder create a folder Data.

In the THIS folder create a class ApplicationDbContext.cs and add the following code

IdentityUser

The Microsoft.AspNetCore.Identity namespace contains the Entity Models of the Identity API. They are

· IdentityRole

· IdentityRoleClaim

· IdentityUser

· IdentityUserClaim

· IdentityUserLogin

· IdentityUserRole

· IdentityUserToke

This class captures the important information about the user.

Registering the DBContext in Startup

The ASP.NET core uses dependency injection framework, where you can simply ask for the required services in the constructor of the class. To do that first we need to configure the services in the startup class.

AddDbContext expects us to provide the Type that we will use for Context class, which is ApplicationDbContext.

Migrations

Next step is to create the database. To do that we use the EF Core Migrations. Run the add-migration and then update-database to create the database.

Result

--

--