Featured Article

Seamless ASP.NET Identity Integration with Existing Database Guide

Kenneth Jul 13, 2026

When it comes to building secure and scalable web applications, managing user authentication and authorization is a critical aspect. ASP.NET Identity, a membership system provided by Microsoft, is a powerful tool for handling user management. But what if you already have an existing database with user data? Can you integrate ASP.NET Identity with your existing database? The short answer is: yes, you can.

ASP.Net Projects with Source Code
ASP.Net Projects with Source Code

Migrating User Data from Existing Database

What is the difference between ASP.NET and ASP.NET Core?
What is the difference between ASP.NET and ASP.NET Core?

Before we start, ensure that your existing database has the necessary user-related tables, such as Users, UserRoles, and Roles. If not, you may need to create or modify your tables to fit the ASP.NET Identity schema. This step involves writing SQL scripts to migrate data from your existing tables to the ASP.NET Identity tables.

For Contoso, they had to create a SQL script to migrate user data from their existing 'Users' table to the ASP.NET Identity 'AspNetUsers' table. Here's an example of how they did it:

Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)

Migrating Users Table

Contoso's 'Users' table was migrated to the 'AspNetUsers' table using a script similar to this:

the complete asp net core q2 2016 chat sheet
the complete asp net core q2 2016 chat sheet

```sql INSERT INTO [dbo].[AspNetUsers] ([Id], [Email], [EmailConfirmed], [PasswordHash], [SecurityStamp], [PhoneNumber], [PhoneNumberConfirmed], [TwoFactorEnabled], [LockoutEndDateUtc], [LockoutEnabled], [AccessFailedCount], [UserName]) SELECT [UserId], [Email], [IsEmailConfirmed], [PasswordHash], [SecurityToken], [PhoneNumber], [IsPhoneNumberConfirmed], [IsTwoFactorEnabled], [LockoutEndDateUtc], [IsLockedOut], [FailedLoginAttempts] FROM [dbo].[Users] ```

Migrating UserRoles and Roles Table

Contoso also had to migrate roles from their 'Roles' table to the 'AspNetRoles' table and user roles from their 'UserRoles' table to the 'AspNetUserRoles' table. They achieved this using similar SQL scripts:

Configuring ASP.NET Identity with Existing Database

Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)

After migrating the data, the next step is to configure ASP.NET Identity to use your existing database. You'll need to update your connection string and create a custom DB context class.

In the Contoso web application, they updated the connection string in the 'web.config' file to point to their existing SQL Server database. Then, they created a custom 'ApplicationDbContext' class that inherits from 'IdentityDbContext'.

Custom DbContext Class

𝗔𝗿𝗲 𝘆𝗼𝘂 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗔𝘂𝘁𝗵𝗲𝗻𝘁𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗔𝗦𝗣 .𝗡𝗘𝗧 𝗖𝗼𝗿𝗲? This guide helped a lot of developers Securing your… | Anton Martyniuk | 41 comments
𝗔𝗿𝗲 𝘆𝗼𝘂 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗔𝘂𝘁𝗵𝗲𝗻𝘁𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗔𝗦𝗣 .𝗡𝗘𝗧 𝗖𝗼𝗿𝗲? This guide helped a lot of developers Securing your… | Anton Martyniuk | 41 comments

Here's an example of the custom 'ApplicationDbContext' class Contoso created:

```csharp public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().Ignore(e => e.User); modelBuilder.Entity().ToTable("Roles"); modelBuilder.Entity().ToTable("UserRoles"); modelBuilder.Entity().ToTable("UserClaims"); modelBuilder.Entity().ToTable("Users"); } } ```

In this class, they specified the 'DefaultConnection' connection string and overwrote the 'OnModelCreating' method to map the ASP.NET Identity entities to their existing tables.

the identity access management iam is shown in blue and white, along with other information
the identity access management iam is shown in blue and white, along with other information
asp net identity with existing database
asp net identity with existing database
A Step by Step Guide for ASP.NET Core Configuration
A Step by Step Guide for ASP.NET Core Configuration
What is ASP.Net and where you can use it? | Geekboots
What is ASP.Net and where you can use it? | Geekboots
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
Create ASP.NET Core Identity SQL Database Asp Dot Net Core Web API - Asp .Net core Web API - Part 2
Create ASP.NET Core Identity SQL Database Asp Dot Net Core Web API - Asp .Net core Web API - Part 2
How to: Access Nested List View or Master Detail View Environment (ASP.NET Core Blazor and Windows Forms) | XAF: Cross-Platform .NET App UI & Web API
How to: Access Nested List View or Master Detail View Environment (ASP.NET Core Blazor and Windows Forms) | XAF: Cross-Platform .NET App UI & Web API
the best identity and access management iam solution for cloud computing - infographical
the best identity and access management iam solution for cloud computing - infographical
the asp net page lifecycle
the asp net page lifecycle

Updating IdentityConfig Class

Contoso also updated their 'IdentityConfig' class to use the custom 'ApplicationDbContext' class. Here's how they modified the 'ConfigureAuth' method:

```csharp public static class IdentityConfig { public const string DefaultConnection = "DefaultConnection"; public static void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationRoleManager.Create); } } ```

With these changes, ASP.NET Identity will use the existing database for managing user authentication and authorization.

Using ASP.NET Identity with Existing Database

Now that you've integrated ASP.NET Identity with your existing database, you can use its features to manage user authentication and authorization in your web application. Here are some examples of how Contoso used ASP.NET Identity:

Creating UserManager and RoleManager

Contoso used the 'UserManager' and 'RoleManager' classes to manage users and roles in their application. They injected these classes into their controllers using the dependency injection container provided by ASP.NET.

```csharp public class AccountsController : Controller { private readonly UserManager _userManager; private readonly RoleManager _roleManager; public AccountsController(UserManager userManager, RoleManager roleManager) { _userManager = userManager; _roleManager = roleManager; } // Other controller methods... } ```

Managing Users and Roles

With 'UserManager' and 'RoleManager', Contoso could easily manage users and roles. Here's an example of how they added a new user to the system:

```csharp [HttpPost] public async Task Register(RegisterViewModel model) { if (!ModelState.IsValid) { return View(model); } var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user.Id, "User"); return RedirectToAction("Index", "Home"); } AddErrors(result); return View(model); } ```

In this example, Contoso creates a new user and adds the "User" role to the new user.

With these steps, Contoso successfully integrated ASP.NET Identity with their existing database and used its features to manage user authentication and authorization in their web application. Your application can follow the same approach to achieve the same results.