Playback starts YouTube’s embedded player (privacy-enhanced mode).
ASP NET Core Identity tutorial from scratch
ChannelkudvenkatDescription from the creator
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
csharp-video-tutorials.blogspot.com/2019/06/asp…
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.
youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?s…
Slides
csharp-video-tutorials.blogspot.com/2019/06/asp…
ASP.NET Core Text Articles & Slides
csharp-video-tutorials.blogspot.com/2019/01/asp…
ASP.NET Core Tutorial
youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQ…
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
youtube.com/user/kudvenkat/playlists?view=1&…
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
Text version of the video
csharp-video-tutorials.blogspot.com/2019/06/asp…
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.
youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?s…
Slides
csharp-video-tutorials.blogspot.com/2019/06/asp…
ASP.NET Core Text Articles & Slides
csharp-video-tutorials.blogspot.com/2019/01/asp…
ASP.NET Core Tutorial
youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQ…
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
youtube.com/user/kudvenkat/playlists?view=1&…
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
About this tutorial
kudvenkat published this 10:12 video tutorial on October 26, 2019. It has been watched 281,252 times on YouTube.
- Channel
- kudvenkat
- Category
- Science & Technology
- Duration
- 10:12
- Published
- October 26, 2019
- Views
- 281,252
- Likes
- 2,267
Tags
Share
This tutorial in other languages
- FRASP NET tutoriel de base Identity à partir de zéro
- EStutorial de ASP NET Núcleo de identidad a partir de cero
- PTtutorial ASP NET Núcleo de identidade a partir do zero
- ITASP NET Nucleo Identità tutorial da zero
- DEASP NET Kernidentität Tutorial von Grund auf neu
- NLASP NET Core Identity les vanuit het niets
- RUASP NET Ядро Идентичность учебник с нуля
- PLTutorial ASP NET Rdzeń Identity od podstaw
- JA最初からASP NETコアアイデンティティのチュートリアル
- ARالبرنامج التعليمي ASP NET الأساسية الهوية من الصفر
- HIखरोंच से एएसपी नेट कोर पहचान ट्यूटोरियल
- IDtutorial ASP NET Inti Identitas dari awal