ASP NET Core Identity tutorial from scratch

preview_player
Показать описание

Text version of the video

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.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

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
}

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
Рекомендации по теме
Комментарии
Автор

The most comprehensive tutorial I've seen about Identity whether paid or not

kareemkaram
Автор

Great course, not only everything works but explains what we are actually using. Going into definitions and discussing them is the way forward!

pochron
Автор

The best tutorial about Identity! I looked a lot of tutorials but only after that I understood what was going on. Thank You
Advice:
Pin all tutorials about Identity in the playlist. It will be easier to find new people in need

meetit
Автор

Venkat sir, you are an amazing teacher! can't describe in words!! Thank you very much for your help and looking forward for the rest of the videos

bilalelmursi
Автор

Thank you. After struggling, I gained deeper understanding on injection, configuration, and application of Identity, as well as everything connects

silvesteronono
Автор

You're a life saver man. Every other tutorial I found never showed how to add identity to existing DBs

CARDSSerus
Автор

from fresher to experience, from 5000 per month to 120000 per month this achievement because of you. only

rajeshmule
Автор

Legend. Thanks so much for this series!

cymrucoder
Автор

nice nice nice, interesting please don't stop you are great trainer

MmMm-tgmq
Автор

Thank you very much
for this amazing series

talkathiriify
Автор

Enchanting Tutorial, thanks a million as soon as I get my bulky salary I'll be donating your channel, your work is simply awesome!
In the meantime, NuGet package should be added before moving further with IdentityDbContext.

sabitkondakc
Автор

Good clear explanations. Thanks very much for this great content!

Beast
Автор

Thank You Kudvenkat Sir. Understand this video...!!!

girishparganiha
Автор

Thankyou very much for great videos as always

vaddiparthivenkatamuralikr
Автор

Man you got a new subscriber. Thank you.

luisgal
Автор

you are the best brother. God bless you and your family

MrTotobey
Автор

@kudvenkat Great tutorial! Love all your videos. I'm still not exactly sure about one thing. Did you create your own User class with..password, email, etc. as properties BEFORE all of the configuration? Or are the "UserIdentity" tables created upon the migration the user entities themselves? I hope this makes sense.

misenheimer_music
Автор

Hello Venkat,

Can we use our own tables instead of tables related to identity db context ? If yes, how to implement it ?

prasannahiremath
Автор

When using ASP.NET Core 3.1 you can add the using statement for IdentityDbContext if you have previously added the following necessary packages for ASP.NET Core Identity with NuGet package manager:


baracuda
Автор

There are no good examples of using ado.net and stored procedures with Asp.Net Core anywhere Mr Venkat. An example on how to use ado.net and stored procedures in Core would help greatly Sir. Thank you so much for all your hard work.

DharmendraSingh-lqrc