Using ASP.NET Core 3.0 Identity With MySQL

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. 
 
Using ASP.NET Core 3.0 Identity With MySQL 
 
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].
 
Using ASP.NET Core 3.0 Identity With MySQL 
 

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:
 
Using ASP.NET Core 3.0 Identity With MySQL 
 
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.
 
Using ASP.NET Core 3.0 Identity With MySQL 
 
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):
  1. "ConnectionStrings": {  
  2.   "DefaultConnection""server=[DB-Server Name];port=3306;database=[DB-Name];uid=[User-ID];password=[Password]"  
  3. }  
Step 4
 
Update ConfigureServices  method of StartUp.cs file with  the following code,
  1. public void ConfigureServices(IServiceCollection services)  
  2.     {  
  3.         services.AddDbContext<ApplicationDbContext>(options =>  
  4.             options.UseMySql(  
  5.                 Configuration.GetConnectionString("DefaultConnection")));  
  6.         services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)  
  7.             .AddEntityFrameworkStores<ApplicationDbContext>();  
  8.         services.AddControllersWithViews();  
  9.         services.AddRazorPages();  
  10.     }  
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.
 
Using ASP.NET Core 3.0 Identity With MySQL
 
Step 6
 
Open the Package Manager Console from View menu of Visual Studio as shown in the following screenshot:
 
Using ASP.NET Core 3.0 Identity With MySQL
 
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,
 
Using ASP.NET Core 3.0 Identity With MySQL
 
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.