ASP.NET Identity Framework 2.0 is integrated with EntityFramework. The entity classes available are Users, Roles, Claims and Logins. The Identity database can be any store – SQL Server, MySQL or Oracle. This article provides guidance on how to integrate ASP.NET Identity 2.0 with a MySQL database.

SQL scripts for MySQL

MySQL has naming conventions which are very different from SQL Server. It uses snake cases for variable names. The script for creating the relevant tables is given below.

User Table

Role Table

User Role Table

User Claim Table

User Login Table

Create a schema by the name: membership_db. Run the above scripts.

Integrating Identity in ASP.NET MVC application

NuGet packages

In a blank ASP.NET MVC project, install the following NuGet packages:

  • EntityFramework
  • MySQL.Data
  • MySQL.Entity
  • ASP.NET Identity Core
  • ASP.NET Identity Extensions

Web.config

Add the following connection string to the web.config:

DbContext

IdentityDbContext provides an out-of-the-box implementation for integrating with SQL Server. Since we are using MySQL, and we used different variable names above, we have to map the entities to the appropriate tables and the properties to the appropriate columns. The DbModelBuilder class has all the mapping methods that we need.

Creating new users

UserManager provides several methods to perform database operations within the Identity framework. First, we create the DBContext. Next, the UserStore is created by passing in the entity classes. Finally, the UserManager is created from the store. To create a new user, pass the new user to the Create method of User manager.

Hopefully, the above code saved some time for you.

ASP.NET Identity Framework 2.0 with MySQL database
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *