ASP NET Core Identity tutorial from scratch

Published: 03 June 2019
on channel: kudvenkat
268,142
2.1k

In this video and in our upcoming videos in this series, we will discuss everything you need, to effectively use ASP.NET Core Identity to implement security related features in your asp.net core application.

Text version of the video
https://csharp-video-tutorials.blogsp...

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
   / @aarvikitchen5572  

Slides
https://csharp-video-tutorials.blogsp...

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogsp...

ASP.NET Core Tutorial
   • ASP.NET core tutorial for beginners  

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenka...


ASP.NET Core Identity

ASP.NET Core Identity is a membership system. It allows us to create, read, update and delete user accounts. Supports account confirmation, authentication, authorization, password recovery, two-factor authentication with SMS. It also supports external login providers like Microsoft, Facebook, Google etc. We will discuss implementing these features in our upcoming videos in this series.

Adding ASP.NET Core Identity Support in ASP.NET Core Application

The following are the steps to add and configure ASP.NET Core Identity

Step 1 : Inherit from IdentityDbContext class

public class AppDbContext : IdentityDbContext
{
// Rest of the code
}

Your application DbContext class must inherit from IdentityDbContext class instead of DbContext class. This is required because IdentityDbContext provides all the DbSet properties needed to manage the identity tables in SQL Server. We will see all the tables that the asp.net core identity framework generates in just a bit. If you go through the hierarchy chain of IdentityDbContext class, you will see it inherits from DbContext class. So this is the reason you do not have to explicitly inherit from DbContext class if your class is inheriting from IdentityDbContext class.

Step 2 : Add ASP.NET Core Identity Services

In ConfigureServices() method of the Startup class, include the following line of code.

services.AddIdentity[IdentityUser, IdentityRole]()
.AddEntityFrameworkStores[AppDbContext]();

AddIdentity() method adds the default identity system configuration for the specified User and Role types.

IdentityUser class is provided by ASP.NET core and contains properties for UserName, PasswordHash, Email etc. This is the clas that is used by default by the ASP.NET Core Identity framework to manage registered users of your application.

If you want store additional information about the registered users like their Gender, City etc. Create a custom class that derives from IdentityUser. In this custom class add the additional properties you need and then plug-in this class instead of the built-in IdentityUser class. We will discuss how to do this in our upcoming videos.

Step 3 : Add Authentication middleware to the request pipeline

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =]
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}

In the Configure() method of the Startup class, call UseAuthentication() method to add the Authentication middleware to the application's request processing pipeline. We want to be able to authenticate users before the request reaches the MVC middleware. So it's important we add authentication middleware before the MVC middleware in the request processing pipeline.

Step 4 : Add Identity Migration

In Visual Studio, from the Package Manager Console window execute the following command to add a new migration

Add-Migration AddingIdentity

This migration contains code that creates the tables required by the ASP.NET Core Identity system.

Error : The entity type 'IdentityUserLogin[string]' requires a primary key to be defined

If you get this error, the most likely cause is that you are overriding OnModelCreating() method in your application DbContext class but not calling the base IdentityDbContext class OnModelCreating() method.

Keys of Identity tables are mapped in OnModelCreating method of IdentityDbContext class. So, to fix this error, all you need to do is, call the base class OnModelCreating() method using the base keyword as shown below.

public class AppDbContext : IdentityDbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Seed();
}
}

Step 4 : Generate ASP.NET Core Identity Tables


Watch video ASP NET Core Identity tutorial from scratch online without registration, duration hours minute second in high quality. This video was added by user kudvenkat 03 June 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 268,14 once and liked it 2.1 thousand people.