ASP.NET Core Identity Deep Dive — Stores
In the previous blog, we talked about the ASP.NET Core identity members. In this post, I wanna explain the Stores in the ASP.NET Core Identity.
Why Identity use Stores?
The only reason that I see for using stores in the identity is implementing the Persistence Layer.
What is the Persistence Layer?
The Persistence Layer is similar to Data Access Layer and there isn’t any huge difference between them. They’re in the middle of database engines and business logics, in other word you isolate them in other layers (Repository pattern is one of them).
The advantages of using the Persistence Layer:
- Easy migration when you want to change your data storage
- Encapsulation the database logic
- Using mock data in Testing cases
So here is a question using the Persistence Layer is a good idea? The answer is yes and no. In some cases that you have to separate the data store logic from the business logic because of the project architecture, it’s a good idea. For example, In Identity, you do not have to use the EFCore and its Providers(SqlServer, MySql, …) maybe you have NoSql data storage like MongoDb and other NoSql databases in the project. But it’s a bad idea when you never change the data storage (You never change it).
Identity Default Stores
In Identity, the default stores are implemented by Entity Framework Core.
When you enable ASP.NET Core Identity in the project by using AddIdentity
, AddIdentityCore
, AddDefaultIdentity
in the startup class, you still haven’t any Store implementation and you will encounter with exceptions like this:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.IUserStore`1[User]’ while attempting to activate ‘UserManager’.
The exception says that there’s not any implementation for one of the Store interfaces. To resolve the issue we need to add Entity Framework Core Stores by using the AddEntityFrameworkStores
method.
What exactly AddEntityFrameworkStores
do?
Here is the source code, if you study it you see that it trying to register its Stores like UserStore
and RoleStore
in IServiceCollection
with the scoped lifetime.
How to use Custom Stores in ASP.NET Core Identity?
You have X or Y datastore in your project, The default store doesn’t support it, don’t worry you can implement your stores.
If you studied the previous post, you know that we have some interfaces for Stores. You can use them for your custom implementation such as the following:
I implemented a simple data store for ASP.NET Core Identity, you can use your database logic in this implementation.
The customization isn’t ready yet, we need to register the new store in the Identity system with using the AddUserStore
method:
There is another method for RoleStore that is AddRoleStore
we use it for registering the custom RoleStore.
Using The Community Stores
There are a lot of Community NuGet packages that represent the Store for different data storages.
Dapper
- https://github.com/grandchamp/Identity.Dapper
- https://github.com/dotnetcore-internal/AspNetCore.Identity.Dapper
MongoDB
- https://github.com/tugberkugurlu/AspNetCore.Identity.MongoDB
- https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore
RavenDB
- https://github.com/maqduni/AspNetCore.Identity.RavenDB
- https://github.com/JudahGabriel/RavenDB.Identity
Cassandra
Redis
Firebase
Conclusion
In this post, we talked about the stores in ASP.NET Core Identity and why they are used in the Identity system. In the end, we’ve learned how we can build our Stores.
English isn’t my main language and I apologize for my grammar mistakes, I appreciate if you tell me the mistakes.