After GitLab Integration With Visual Studio 2019 now it’s time to implement the Identity. Which is a kind of membership or user management system for ASP.NET Core. Identity is widely used by ERP, CMS and custom applications developed in ASP.NET MVC or CORE. Even that I prefer Identity in applications which I developed, to optimize time and shorter the code.
Before getting started with Identity, you need to understand what you will get.
- User Management – AspNetUsers
- Role Management – AspNetRoles
- User Claim (User Permission) – AspNetUserClaims
- Role Claim (Role Permission) – AspNetRoleClaims
- User Role Mapping – AspNetUserRoles
There are other tables too which are created on implementation of Identity, but we will not use them.
- AspNetUserLogins
- AspNetUserTokens
These tables are used in sense when you have social logins integration in your application.
Install Identity in ASP.NET Core Project
By default there no built-in UI for Identity in Core. But we have that via Nuget Package Manager.
- Open Nuget Package Manager
- Search for (Microsoft.AspNetCore.Identity.UI)
- Install Compatible version
After this it will create and area and you don’t need to do anything. It will automatically add dependency injection in Startup.cs Class. If you are wanted to use custom User & Role then you need to override default dependency injection.
Default:
1 2 3 |
services.AddDefaultIdentity<IdentityUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); |
Custom:
1 2 3 |
services.AddDefaultIdentity<ApplicationUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); |
After this you have to apply following commands to integrate Identity.
1 |
Update-Database |
It will sync your database with Identity Tables.
Identity Default Dependency Injection:
In case of default if you will execute the application, you are finished with Identity because your user management system is successfully configured and ready to use. You can look into your newly created tables to understand their hierarchy.
Identity Custom Dependency Injection:
In this case you also have to add your custom class to DbContext or ApplicationDbContext.
1 2 3 4 5 6 7 |
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } |
Like above code you can see, we have utilized our custom User class in IdentityDBContext which will now operate whole user management system on base of our new class. If you have to implement custom role along with custom user you will have to add following dependency injection in Startup.cs Class.
1 2 3 |
services.AddDefaultIdentity<ApplicationUser, ApplicationRole>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); |
After this modify your ApplicationDbContext.
1 2 3 4 5 6 7 |
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } |
In this way you can have customized user & role management system, rest of the things remain same.
Conclusion
With identity you can have built-in and mature user-role management system in few clicks. You don’t need to waste your time on custom logic’s. If will give you complete access over users and their roles. As you have learned we have created default and customized identity management this tutorial. For more clarification you can watch the following video tutorial.