ASP.NET Identity Framework 2.0 is integrated with EntityFramework and provides entity classes to deal with Users, Roles, Claims, and third-party Logins. The Identity database can be any store – SQL Server, MySQL or Oracle. This article will provide 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. 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 out-of-the-box implementation for integrating with SQL Server. Since we are using MySQL, and the naming conventions are different, we have to map the entities to the appropriate tables, and the properties to the appropriate columns.

Creating new users

UserManager provides several methods to manage users, roles, logins, claims within the Identity framework. The integration code to create users is as follows:

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 *