This video is available to students only

Configuring Identity and Migration

In this lesson, we're going to configure and migrate

Configuring Identity and migration#

Now to complete the setup, we need to go to the Startup class. Inside the configureServices method, we need to add another service for Identity. Let's do it on top because it doesn't demand any order. To make the Identity service available in our API project, let's add reference to the Entity project.

Now, we will use services.AddIdentityCore which will take a type that is user. Although we could mention IdentityUser here — the one User class is deriving from, that will restrict us to use only the existing properties. Adding User class here gives us scope to add more properties if required in the future. Now we can chain this with roles because we need them as well. I can write AddRoles which will take a type IdentityRole. We also need to mention the name of our store, for which we will use AddEntityFrameworkStores, which will take a type of StoreContext class. Adding the service will create migration which will create new multiple identity related tables for us automatically.

Below this, we also need to add two more services; authentication and authorization. Although order doesn't really matter inside the configure services method, it's important to add these two services after adding the Identity service. So let's mention services.AddAuthentication and services.AddAuthorization. Adding Identity gives us access to the userManager and signinManager which we can take advantage of. I haven't really added another file for the new users, so we can simply type it inside our StoreContextSeed class.

API/Startup.cs

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