How to Implement ASP.NET Identity in MVC 5 with Integer Primary Keys and Custom User Fields
ASP.NET Identity is a powerful and extensible authentication and authorization system used in modern .NET applications. This guide walks through implementing ASP.NET Identity in ASP.NET MVC 5 with support for integer-based primary keys and custom user profile columns.
🧑💼 Custom AspNetUsers
Table with Integer Primary Key
Here’s a customized version of the AspNetUsers
table to suit enterprise-grade user data needs:
- Integer primary key (
Id
) - Personal details like
FirstName
,LastName
,Mobile
- Multiple address fields
- Custom organizational fields like
OrgPosition
,Department
,Service
- Account control features like
PasswordExpires
,ThemeMode
, andPendingApproval
🔧 Step-by-Step Identity Integration in ASP.NET MVC 5
📦 1. Install Required NuGet Packages
Install-Package Microsoft.AspNet.Identity.Core
Install-Package Microsoft.AspNet.Identity.EntityFramework
Install-Package Microsoft.AspNet.Identity.Owin
Install-Package Microsoft.Owin.Host.SystemWeb
🏗️ 2. Create Custom ApplicationUser
Class with Integer Keys
Extend IdentityUser
with int
keys and custom fields:
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Mobile { get; set; }
public string RoleName { get; set; }
public string MemorableWord { get; set; }
public DateTime StartDate { get; set; } = DateTime.Now;
public DateTime? PasswordExpires { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
🔁 Supporting Identity Classes
public class ApplicationUserLogin : IdentityUserLogin<int> { }
public class ApplicationUserRole : IdentityUserRole<int> { }
public class ApplicationUserClaim : IdentityUserClaim<int> { }
public class ApplicationRole : IdentityRole<int, ApplicationUserRole>
{
public ApplicationRole() { }
public ApplicationRole(string name) { Name = name; }
}
🧱 3. Configure AppDbContext
public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int,
ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public AppDbContext() : base("YourConnectionStringName") { }
public static AppDbContext Create() => new AppDbContext();
}
⚙️ 4. Set Up OWIN in Startup.Auth.cs
app.CreatePerOwinContext(AppDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
📝 5. User Registration Example
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.UserName,
FirstName = model.FirstName,
Email = model.Email,
StartDate = DateTime.Now
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
return RedirectToAction("LogonForm", "Account");
}
AddErrors(result);
}
return View(model);
}
🔑 6. User Login with Password Expiry Check
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> LogonForm(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
return View(model);
var user = await UserManager.FindByNameAsync(model.UserName);
if (user?.PasswordExpires < DateTime.Now)
return RedirectToAction("ResetPassword", "Account");
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, false);
if (result == SignInStatus.Success)
{
await SignInUserWithClaims(user, model.RememberMe);
return RedirectToAction("Dashboard", "Home");
}
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
📦 7. Entity Framework Code First Migration
Enable-Migrations
Add-Migration InitIdentitySchema
Update-Database
✅ Final Thoughts
With this approach, you’ve implemented a fully functional ASP.NET Identity system using integer primary keys and an extended user model. This structure is ideal for enterprise applications requiring custom fields, better scalability, and future-proofing your authentication layer.
Need role management, user permissions, or claims? This setup provides the perfect foundation.
📌 Tags:
- asp.net identity mvc 5
- identity integer primary key
- custom aspnetusers table
- mvc authentication
- entity framework code first identity
0 Comments