0:02 / 10:54 Let us try to enable ASP.NET Core Identity in Web API controllers.
We have already learned ASP.NET Core Identity earlier in the previous sections
in combination with MVC controllers. So we are not going to reintroduce all the concepts of
Identity here. Just we are trying to quickly use Identity in combination with Web API.
Here you have to create the dbContext, IdentityStore, IdentityManager which provides
DataAccessLayer, Repository and BusinessLogic respectively. And you have to install essential
NuGet packages. The plan is we have to create the Identity entities in this Core project
and creating the dbContext as a part of the infrastructure. Enabling Identity in the Web
API project. At first you have to install essential NuGet packages.
So right click on Dependencies of the Core project, Manage NuGet Packages,
Browse and search for Microsoft.AspNetCore.Identity.EntityFrameworkCore.
This package is required in order to enable Identity in your project.
So select this Microsoft.AspNetCore.Identity.EntityFrameworkCore.
Select the current version, as of me that is 7.0.4. Install and OK.
And you have to install exactly the same package even in the infrastructure project.
So close this NuGet package manager window. Right click on Dependencies of the infrastructure
project, Manage NuGet Packages and in the Browse search for the same
Microsoft.AspNetCore.Identity.EntityFrameworkCore. Select the same version 7.0.4 and install.
OK. We have installed this package even in the infrastructure project.
So in the Core project, right click on the Core project, add a new folder. Of course the folder
name can be anything. Right click on Identity, add a new item. Select Code Class. File name is
ApplicationUser. You are going to create ApplicationUser and ApplicationRole entity
classes in this Identity folder in the Core project. Make it a public class. The namespace
name is CitiesManager.Core.Identity and inherit from IdentityUser of the data type of the ID.
You can add additional properties here that you want to store for every user.
For example, I would like to store person name apart from the user name.
Here I am going to use email address as user name. So separate person name. Similarly,
in the same Identity folder in the Core project, right click, add new item,
class, ApplicationRole. So in the ApplicationRole class, make it as a public class,
inherit from IdentityRole. The data type for ID is GUID. Import the namespace Microsoft.ASPNetCore.Identity
and you can add additional properties if required that you want to store for every role.
For example, you would like to store the details of permissions of every particular user role.
You can add essential related properties in this ApplicationRole class.
Currently, we are not adding anything. Now in the Infrastructure project, go to the
ApplicationDB context that we have created already. Instead of inheriting from DB context,
which is the normal Entity Framework DB context, instead of this, inherit from IdentityDB context.
Here you have to mention the data types for user. In our case, it is ApplicationUser.
And secondly, the class name of the role, that is ApplicationRole and the class name of
the user ID, that is GUID in this case. Import the namespace Microsoft.ASPNetCore.Identity.EntityFrameworkCore.
Also import our CitiesManager.Core.Identity namespace because in this namespace only
we have created these classes, that is ApplicationUser and ApplicationRole.
So it creates the essential DB sets that are required for ASPNetUsers table,
ASPNetRoles table and all the related identity tables. So you need not add any additional DB sets
apart from the one that you want. Okay, now in the program.cs file, we have to enable identity.
So at the bottom of all these existing services, that is just above this build method, you can add
essential services. Hey services, add identity in this application. I know you are asking the
data type for user. In my case, it is ApplicationUser imported from CitiesManager.Core.Identity namespace
and the data type for role is ApplicationRole. And I know that you can offer a lambda expression
that contains options as parameter. And in these options, I can configure essential details
or settings related to identity. For example, password complexity.
I want the password should be simple. So required password length is
at least five characters could be enough, in my case at least. And non-alphanumerical characters,
that is symbols, is not required for my case. So make it false. And also uppercase letter is
not essential. So require uppercase equal to false. And also lowercase is equal to true.
At least one lowercase letter is required. And then require digit equal to true.
Okay, these are my settings. Okay, lowercase, C is lowercase, that’s fine. And then on top of this,
since the AddIdentity returns the IdentityBuilder, on top of this IdentityBuilder,
you can add additional calls, that is AddEntityFrameworkStores.
Yes, I would like to store all the DB sets in the ApplicationDB context.
In other words, the user store class internally should use ApplicationDB context
in order to manipulate the tables. And add the default token providers.
You need to import essential namespaces that are mentioned here. For example,
Microsoft.ASP.NET Core.Identity, Infrastructure.DatabaseContext, etc.
And then as a continuation, you need to add the user store. The built-in class is UserStore.
We have not created any child class for the same UserStore with ApplicationUser,
that is the datatype for users. And also it asks for the datatype of role, that is ApplicationRole.
And then datatype of the DB context, that is ApplicationDBContext.
And the datatype of UserId, that is GUID in this case.
And then on top of this, you are going to add the role store in the same way as above.
But this time you have to mention the datatype for ApplicationRole and ApplicationDBContext.
And the datatype of roleId, that is GUID in this case.
Okay. Import the namespace, that is Microsoft.AspNetCore.Identity.EntityFrameworkCore.
Then you can reference this UserStore class. And here it is, role store.
Okay, successfully we have enabled identity in this project.
And we will try to create registration and login functionality in further lectures.
The plan here is that I will try to show you how do you enable authentication and authorization
in combination with WebAPI controllers using JWT authentication, which is the most popular
and recommended way to enable authentication for WebAPI applications. And also in addition to this,
at the bottom, enable authentication by using UseAuthentication method.
The order of usage must be like this, use routing, use codes, and then use authentication,
then authorization, then only map controllers. Here the order matters.
Okay, in the next lecture, we are going to create endpoint for registration of the user.
Stop Play Play Play Play Play Play Play Play Play Play Start Start Start