ASP NET Core Identity tutorial from scratch



ASP NET Core Identity tutorial from scratch

ASP NET Core Identity tutorial from scratch

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.blogspot.com/2019/06/aspnet-core-identity-tutorial-from.html

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.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Slides
https://csharp-video-tutorials.blogspot.com/2019/06/aspnet-core-identity-tutorial-from_3.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

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

Comments are closed.