Overview
As per its official documentation, IdentityServer is a free, open source OpenID Connect and OAuth 2.0 framework for ASP.NET Core. Founded and maintained by Dominick Baier and Brock Allen, IdentityServer4 incorporates all the protocol implementations and extensibility points needed to integrate token-based authentication, single-sign-on and API access control in your applications.
However all the examples of implementing the identity server in ASP.NET Core 3.0 use MS SQL server as their back-end database, which is primaraly used by corporations, not indie developers or small organizations becuase of the huge license fee involved. In this article we will learn an alternative to this; we will learn how to use MySQL as a back-end database for identity server implementation in ASP.Net core 3.0 in a step by step fashion
Setting Up a New Project with Identity
Create a new ASP.NET Core MVC project in Visual studio by following the steps mentioned in official documentation. un Step 4 of the blog. Instead of choosing Web Application choose Web Application (Model-View-Controller), instead of No Authentication choose Individual authentication by clicking on the change highlighted in the below image.
Or if you are comfortable using Command Prompt then use the following command in Visual Studio Command Prompt,
dotnet new mvc --auth Individual
Adding Identity to an Existing ASP.Net Core Application
If you are like me, and already have an ASP.NET Core MVC application without Identity Implemented then you just need to add Microsoft.AspNetCore.Identity nuget package from nuget package manager window (shown below) which will appear by right clicking on the Project file and selecting the option Manage Nuget Packages [As shown in the image of Step-1 of Steps to use MySQL as back-end database].
Steps to use MySQL as Back-end database
Step 1
Open Nuget package manager in Visual Studio by right clicking on the project file and select Manage Nuget Packages as shown in the below screenshot:
Step 2
In the Browse tab of Nuget package manager, search for package Pomelo.EntityFrameworkCore.MySql this is the Entity Framework Core version with MySQL provider implemented as mentioned in the provider list maintained by Microsoft.
Step 3
Change the connection string in appsettings.json file with your MySQL Db details, something like the following (Use your DB details for the Bold Parts):
- "ConnectionStrings": {
- "DefaultConnection": "server=[DB-Server Name];port=3306;database=[DB-Name];uid=[User-ID];password=[Password]"
- }
Step 4
Update ConfigureServices method of StartUp.cs file with the following code,
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseMySql(
- Configuration.GetConnectionString("DefaultConnection")));
- services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
- .AddEntityFrameworkStores<ApplicationDbContext>();
- services.AddControllersWithViews();
- services.AddRazorPages();
- }
Step 5
Delete the Migrations folder (highlighted in below screenshot) from the project by right clicking on it and selecting Delete from the Context menu as the default Migrations code is written based on SQL Server.

Step 6
Open the Package Manager Console from View menu of Visual Studio as shown in the following screenshot:

Step 7
Execute the following command to create new migrations code based on MY SQL database. In simple words migration code is the code responsible to create the database (if not present), tables and stored procedures used by Identity server.
Add-Migration InitialCreate
Once the above code is executed you will see the Migration folder created on the root of the project as shown in the following screenshot,

Step 8
Now execute the following command to apply new migration code to the database.
PM> Update-Database
That's it our ASP.Net Core application is ready with identity server implemented using MySQL as back-end database. You can execute the application and see the login and Register pages working just like SQL Server based Identity code.
In this article we learned, how to use MySQL as a back-end database for identity server implementation in ASP.Net core. The code of this sample application is available here at Github in CoreIdentityEx Project folder.

Lulama NdukwanaPosted May 21, 2021, 4:03 PM
Thanks for this.
Alexander SvenningPosted Mar 23, 2021, 2:04 PM
Is this possible to do with existing mysql-db?
Gcfr GucPosted Nov 24, 2020, 5:53 PM
Please how did you get the ApplicationDbContext Class in the Data folder
Tom BPosted Jul 4, 2020, 9:07 AM
Thank you for posting this. When I execute step 8, I get the following error: "Failed executing DbCommand (286ms) [Parameters=[], CommandType='Text', CommandTimeout='30']CREATE TABLE `AspNetRoles` ( `Id` varchar(255) CHARACTER SET utf8mb4 NOT NULL, `Name` varchar(144) CHARACTER SET utf8mb4 NULL, `NormalizedName` varchar(144) CHARACTER SET utf8mb4 NULL, `ConcurrencyStamp` longtext CHARACTER SET utf8mb4 NULL, CONSTRAINT `PK_AspNetRoles` PRIMARY KEY (`Id`) ); Specified key was too long; max key length is 1000 bytes; if you know how to address this, I'd much appreciate if you let us know
AntonioNovoaPosted May 7, 2020, 6:49 PM
Excellent post Kumar.
Ben HayatPosted Jan 27, 2020, 1:46 AM
But the big question is, with all the cyber attacks, is it safe and secure to store users' credentials on-premise using IdentityServer4 regardless of DB, than using something like Azure B2C or Okta orAuth0, where users' data is stored and protected by better security than a small company can provide? It's a big liability to keep user's credentials in-house. But no one seems to point out user's data storage location and securing it.