Setting up identity

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

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:06] 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.

    [00:07 - 00:11] It comes out of the box with ASP.NET core. We just need to tie it to our project.

    [00:12 - 00:25] 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. So without any further delay, let's start configuring it.

    [00:26 - 00:41] We need to open the newkit gallery. So if you're on Mac, press command shift P and open the newkit gallery. And first of all, we need to search for Microsoft.asp net core.authentication.j wtbearer.

    [00:42 - 01:11] So let's type Microsoft.asp net core.authentication.jwtbearer. Although the version 6 is now available, but we will stick to our current version, which is 5.2.6.

    [01:12 - 01:17] And we need to install it inside our API project. So let's click on install.

    [01:18 - 01:29] Let me tell you something about this package. This is a middleware that enables an application to receive an open ID connect where a token.

    [01:30 - 01:40] In short, this provides middleware to allow validating and extracting the JSON web tokens from a header. Now we need to install an identity package.

    [01:41 - 02:03] So what we can do is first of all, let's close it, press any key and it'll automatically cancel it. And now we can search for Microsoft dot asp net core.identity.

    [02:04 - 02:17] And this time we need Microsoft dot asp net core.identity.entity framework. And this needs to be installed inside the entity project with the same version 5.0.6.

    [02:18 - 02:31] And let's click on install. This will help us create the migrations and the tables inside our database.

    [02:32 - 02:41] Now that we have installed the dependencies, I can simply close it. Now we can create a new user class inside the entity project.

    [02:42 - 02:48] So I'll open entity and create a new class. And let's call it user.

    [02:49 - 02:54] Let's close the terminal. Now we don't have to add any property by ourselves.

    [02:55 - 03:02] It already comes with a lot of handy properties. But to make those properties available, we need to derive it from the identity user.

    [03:03 - 03:16] So what I'll do is derive it from identity user. So let's import Microsoft asp net core dot identity.

    [03:17 - 03:25] If you want to look at the properties it provides you, you can go inside the identity user. And again, we can go inside this identity user.

    [03:26 - 03:36] You will find all these properties here. So we have two factor enabled, the phone number, email, password hash, user name, id, etc.

    [03:37 - 03:46] So that's what we get out of the box when we derive from the identity user. Now we also need to add the tables for the users.

    [03:47 - 03:54] So ideally we will go to our store context file and create new DB set. So let's go to the store context.

    [03:55 - 04:06] But this time they will not create any DB set. They will simply derive the store context from identity DB context.

    [04:07 - 04:17] So rather than using DB context, I can write identity. After deriving it from the identity DB context, we need to give it a type of the user.

    [04:18 - 04:28] So we have created the user class. Now we can simply import it using Microsoft asp net core dot identity dot entity framework core.

    [04:29 - 04:39] And if you're not able to see the automatic import, you can just close the project and open it again. Or you can just go inside the project and try restoring it.

    [04:40 - 04:47] In our application, we will have two types of users, students and instructors. This is called roles in identity framework.

    [04:48 - 04:55] So let's start with it. We can simply use the on model creating method which already exists below the existing code.

    [04:56 - 05:05] We will write builder dot entity. And we will mention the identity role because we want to create the roles.

    [05:06 - 05:14] And now we will write has data. Inside which we can create new instances of the identity role.

    [05:15 - 05:29] So we will write new identity role. And inside the curly brackets, we will write name of the role which will be student.

    [05:30 - 05:43] Let's import new identity role from identity. And now write the normalized name property which will be student in caps.

    [05:44 - 05:51] We can copy it one more time. And this time we will make it for the instructor.

    [05:52 - 06:00] So let's write instructor and here. We will write instructor in capitals.

    [06:01 - 06:09] And here let's pass a semicolon. Well, doing this will create a new table with these roles populated.

    [06:10 - 06:12] Let's add this to our startup class in the next lesson.