This video is available to students only

Setting up identity

In this lesson, we're going to set up ASP identity

Now is the time when we will set up our ASP Identity on our API. It's not something we need to create from scratch. It comes out of the box with ASP.Net Core Identity. We just need to tie it to our project. You don't have to worry about the security as it is provided by Asp.net, which has spent a lot of time providing the best possible result to its users. Let's start configuring it.

We need to open the Nuget gallery and search for Microsoft.AspNetCore.Authentication.JwtBearer. This is a middleware that enables an application to receive an openId connect bearer token. In short, this provides middleware to allow validating and extracting JWT bearer tokens from a header. We need to install this inside our API project, and the version should be 5.0.6. Now we need to install the identity package, so we can search for Microsoft.AspNetCore.Identity.EntityFrameworkCore, and this needs to be installed inside the Entity project, with the same version 5.0.6 . This will help us create the migrations and the tables in our database.

Now that we have installed the dependencies, we can create a new user class inside Entity project. We don't have to add any property by ourselves now; it already comes with a lot of handy properties, but to make those properties available, we need to derive it from IdentityUser. If you want to look at the properties it provides you, you can go inside the IdentityUser. If we go inside it again, you will find all the properties. So we have TwoFactorEnabled, PhoneNumber, Email, PasswordHash, UserName, Id etc. That's what we get out of the box when we derive from the IdentityUser. Now we also need to add the tables for the users. Ideally, we will go to our StoreContext file and create a new dbset, but we will not do that here. We will simply derive the StoreContext from IdentityDbContext rather than deriving it from the DbContext. With this, we will also give it a type of the user class that we just created. Although, we don't have any custom properties, but if we had them, it would create the properties as part of the migration and the column of the table.

Start a new discussion. All notification go to the author.