How to Customize Asp.NET Identity Core with External Database Storage step by step

ASP.NET Core Identity is designed to enable us to easily use a number of different storage providers for our ASP.NET applications. We can use the supplied Identity providers that are included with the .NET Framework, or we can implement your own providers.

There are two primary reasons for creating a custom Identity  provider.

We need to store Identity information in a data source that is not supported by the Identity providers included with the .NET Framework, such as a MysQL database, an Oracle database, or other data sources.
We need to manage Identity information using a database schema that is different from the database schema used by the providers that ship with the .NET Framework.

A common example of this would be to use authentication data that already exists in a SQL Server database for a company or Web site.
In this tutorial, we are going to implement and configure a custom Identity Provider using ASP.NET MVC Core and IndentityCore

Create Asp.NET Core   with individual user account Project

STARTUP

Open Package Manager Console and run Update-Database.

UpdateDatabase

The migration schema in folder Data/Migrations will be applied to create the database in localDB.

The connectionString is in file appSettings.json

ConnectionString

 

Migration

Open Server explorer to view generated database tables :  AspNetUsers, AspNetRoles, AspNetRoleClaims, AspNetUserClaims, AspNetUserLogins, AspNetUserRoles,AspNetUserTokens

Database

Schema

Lets press F5 to run our projet and click on << Register >> link so as to create a new account as follow

Lets enter our email and our password and click register button

register

A new record is added to aspNetUsers table

TableData

Our application is now ready to use an external storage, on the next section, we will show you how to customize the external database storage

Create Identity Library Project

Lets create a Class Library project and name it IdentityCore.Library

Right click reference and select Manage Nuget Packages

Untitled

Install package Microsoft.AspNet.Identity.Core  and Microsoft.AspNetCore.Identity.EntityFrameworkCore

Nuget

Create an MyIdentityUser class to hold user informations, it inherits from IdentityUser  and an  MyIdentityRole  class that inherits from IdentityRole<string>

MyIdentityRole class hold user roles informations

we will implement MyUserStore to  customize our storage provider

Lets Create a MyRoleStore Class and implement IRoleStore

Configure Asp.Net client to target our custom Identity Library

Open Startup.cs file and remove the following code :

Paste the following code : insteadof  using ApplicationUser and  IdentityRole, we are going to use : MyIdentityUser and MyIdentityRole

And finally replace all usage of  ApplicationUser  by  MyIdentityUser

Press F5  to run application,  and  register a new account

Thats all , we are using our new framwork to manage our custom storage logic

 Exception

Download source code

Best regards.

Gora LEYE

I’m a .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks,