Configuring Identity and Migration
In this lesson, we're going to configure and migrate
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:13] Now, to complete the setup, we need to go to the startup class. So let me write startup and inside the configure services method, we need to add another service for identity.
[00:14 - 00:24] Let's do it on top because it doesn't demand any order. To make the identity service available in the API project, we need to add the reference to the entity project.
[00:25 - 00:45] So let's open the terminal and let's go inside the API project and here we need to add the reference. So we'll type.net add reference and this is going to be the entity project and we're done.
[00:46 - 01:17] Now let's close the terminal and here we will use the services dot add identity call, which will take type, which is user and let's import user using entity. Although we could mention the identity user here, which the user class is already deriving from, but that will restrict us to use only existing properties.
[01:18 - 01:26] Adding user class here will give us a scope to add more properties if required in the future. Now let's change this with the roles because we need the roles as well.
[01:27 - 01:43] So what I'll do is write dot add roles and this will take a type identity role. So let's mention identity role and we need to import identity role from a spin et card or identity.
[01:44 - 02:04] We also need to mention the name of our store for which we will chain it with dot add entity framework stores and this will take a type of our store context. Adding the service will create migration, which will create multiple new identity related tables for us automatically.
[02:05 - 02:18] So this we also need to add two more services, which is authentication and authorization. Although order doesn't really matter inside the configure services method, but it's important to add these two services after adding the identity service.
[02:19 - 02:43] So let's mention below this services dot add authentication followed by services dot add authorization. Adding identity gives us access to the user manager and sign in manager, which we can take advantage of.
[02:44 - 02:49] I haven't really added another file for the new users. We can simply type it inside our store context seed class.
[02:50 - 02:56] So let's go there. Store context seed and let me close the user class here.
[02:57 - 03:14] Now inside the store context seed class, we can inject the user manager, which will be of type user. So here let me write user manager, which will be of type user and let's call it user manager.
[03:15 - 03:27] Let's import user manager using identity. And like we were using context to check if the courses are available, we can use the user manager to check if there are any users.
[03:28 - 03:44] On top, what we can do is we can check if no user manager dot users dot any. If this is true, we will create a new variable student, which will be a new user.
[03:45 - 03:56] So let me write, war student and this will be a new user. We have already written the user class.
[03:57 - 04:05] So let's put it down here. And here we will mention the username and let's give it a name student.
[04:06 - 04:11] Very simple. And let's also give it an email and email will be again.
[04:12 - 04:20] Let's keep it simple. So I will write student at the rate test.com.
[04:21 - 04:27] And let's pass a semicolon here. Now we will create a new user.
[04:28 - 04:42] So here, let's write a weight user manager dot create a sync and this will take student as the first parameter. And as a second parameter, it will accept a password.
[04:43 - 04:50] So let's keep it simple and I will name it password at the rate one, two, three . This is just a test user.
[04:51 - 04:58] Don't use a simple password in production. Now we have created a new user, but haven't assigned a role yet.
[04:59 - 05:19] For that, we will use again, let's go down and use a weight user manager dot add to roll a sync again. It takes student and as a second parameter, it takes the role and this is a role student.
[05:20 - 05:29] Let's create another user who will be an instructor. So rather than typing the code again, let me copy it and paste it below.
[05:30 - 05:41] Now we will replace the student with instructor. So let me select it and write instructor.
[05:42 - 05:55] And here as well, let's give it a role instructor. But if you're logging in as an instructor, you should also be able to access the courses, right?
[05:56 - 06:13] So what we can do is we can provide multiple roles. Here we will write new array and we will mention instructor as well as a student.
[06:14 - 06:18] And it's not a string. So let's get rid of it.
[06:19 - 06:24] And this should be add to roles a sync. And this will resolve the error.
[06:25 - 06:43] And by the time they will seed the users in the database, the rules student and instructor will be already available as mentioned inside the store context class here. So by the time we reach here, the rules student and instructor will be already present.
[06:44 - 06:50] So we need to worry about that. We have now added user manager inside this class, which means we need to register it inside the program class.
[06:51 - 07:08] So let's go to the program class. And what we can do is let's copy it and paste it and replace context with user manager.
[07:09 - 07:23] And here as well, we will mention user manager of type user. And we need to import user manager using ASP net core identity.
[07:24 - 07:36] Also the user using entity. And now we need to pass user manager to the store context seed dot seed async method like that.
[07:37 - 07:41] It's not time to make the migration. Make sure the server is not running.
[07:42 - 07:53] Let's come out of the API project. And here let's write dot net if migrations add and let's call our migration identity.
[07:54 - 08:00] The starter project is API. And the project is infrastructure.
[08:01 - 08:15] And let's press enter. It is now created the migration.
[08:16 - 08:23] Let's take a look inside the migrations folder inside infrastructure. And let's see the identity migration.
[08:24 - 08:44] As you can see, we have table for users, table for the rules, table for rule claims, table for user logins, ASP net user rules, and user tokens, ASP net roles. We also have method to insert rules like this.
[08:45 - 08:48] So here we can see the student role that we have created. And here we see the instructor role.
[08:49 - 08:55] We haven't written any code for these services except the rules. They all come out of the box with identity.
[08:56 - 09:03] Now we just have to restart the server. And it will hopefully create the identity tables for us before running dot net watch run.
[09:04 - 09:10] I noticed something that here we are adding student instead of the instructor. So let's make it instructor.
[09:11 - 09:16] Otherwise, it will give us an error. Now we can open the terminal again inside the API project.
[09:17 - 09:26] We can run dot net watch run. And looks like we don't have any error.
[09:27 - 09:37] Now we can open the database using command shift P open database and we will open the learn if I table. And here, let's look at the users.
[09:38 - 09:39] And we have two users. One is student.
[09:40 - 09:43] Another one is an instructor. Now that's working fine.
[09:44 - 09:49] Also if we check the rules. So we see we have two rules, a student and instructor.
[09:50 - 09:56] Perfect. Now that all of this is working fine, let's start working on the APIs in the next lesson.