πŸ‘€ User Management & Identity in ASP.NET Core (ASP.NET Core Identity)

Samim.Hossain
Samim Hossain
Published on Mar, 08 2026 3 min read 0 comments
image

Introduction

Almost every serious application needs user management:

  • User registration & login
  • Password hashing
  • Role management
  • Secure authentication

ASP.NET Core provides a powerful built-in system called ASP.NET Core Identity that solves these problems securely and professionally.

In this article, you’ll learn:

  • What ASP.NET Core Identity is
  • How it works internally
  • How to implement user registration & login
  • How Identity works with JWT
  • Best practices for real projects

This is Week 8 of our ASP.NET Core weekly series.

What Is ASP.NET Core Identity?

ASP.NET Core Identity is a membership system that handles:

  • Users
  • Passwords
  • Roles
  • Claims
  • Security tokens

It is:

  • Secure by default
  • Database-backed
  • Highly customizable
  • Production-ready

πŸ“Œ Identity is not just login β€” it’s a full user management framework.

Identity vs Custom Auth (Important)

| Feature          | ASP.NET Core Identity | Custom Auth           |
| ---------------- | --------------------- | --------------------- |
| Password hashing | βœ… Built-in            | ❌ Manual              |
| User lockout     | βœ… Yes                 | ❌ Usually missing     |
| Role management  | βœ… Yes                 | ❌ Manual              |
| Token support    | βœ… Yes                 | ❌ Complex             |
| Security updates | βœ… Microsoft           | ❌ Your responsibility |

πŸ‘‰ Use Identity unless you have a very strong reason not to.

How ASP.NET Core Identity Works

High-level flow:

Controller β†’ UserManager β†’ Identity β†’ Database
                     ↓
                Password Hasher

Key components:

  • UserManager<TUser> β†’ user operations
  • SignInManager<TUser> β†’ login/logout
  • RoleManager<TRole> β†’ roles
  • Identity tables β†’ database storage

Step 1: Install Required Packages

dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore

(Usually added automatically in templates)

Step 2: Create Identity User

You can extend the default Identity user.

using Microsoft.AspNetCore.Identity;

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
}

βœ” Adds custom fields
βœ” Keeps Identity security intact

Step 3: Configure Identity with EF Core

DbContext

public class AppDbContext : IdentityDbContext<ApplicationUser>
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }
}

Program.cs Configuration

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<AppDbContext>()
    .AddDefaultTokenProviders();

Step 4: Identity Database Tables

When you run migrations, Identity creates tables like:

| Table            | Purpose         |
| ---------------- | --------------- |
| AspNetUsers      | Users           |
| AspNetRoles      | Roles           |
| AspNetUserRoles  | User ↔ Role     |
| AspNetUserClaims | Claims          |
| AspNetUserLogins | External logins |

πŸ“Œ Never edit these tables manually.

Step 5: User Registration

Register DTO

public class RegisterDto
{
    public string Email { get; set; }
    public string Password { get; set; }
}

Registration Controller

[HttpPost("register")]
public async Task<IActionResult> Register(RegisterDto model)
{
    var user = new ApplicationUser
    {
        UserName = model.Email,
        Email = model.Email
    };

    var result = await _userManager.CreateAsync(user, model.Password);

    if (!result.Succeeded)
        return BadRequest(result.Errors);

    return Ok("User registered successfully");
}

βœ” Password is automatically hashed
βœ” Security rules are enforced

Step 6: User Login

[HttpPost("login")]
public async Task<IActionResult> Login(LoginDto model)
{
    var user = await _userManager.FindByEmailAsync(model.Email);

    if (user == null)
        return Unauthorized();

    var valid = await _userManager.CheckPasswordAsync(user, model.Password);

    if (!valid)
        return Unauthorized();

    // Generate JWT here
    return Ok("Login successful");
}

πŸ“Œ Identity handles password verification securely.

Step 7: Identity + JWT (Best Practice)

Real-world APIs use:

  • ASP.NET Core Identity β†’ user management
  • JWT β†’ stateless authentication

Flow:

  1. Identity validates user
  2. JWT generated with user ID + roles
  3. Client stores JWT
  4. API validates JWT on every request

βœ” Scalable
βœ” Secure
βœ” Cloud-ready

Step 8: Role Management

Create Role

await _roleManager.CreateAsync(new IdentityRole("Admin"));

Assign Role

await _userManager.AddToRoleAsync(user, "Admin");

Secure Endpoint

[Authorize(Roles = "Admin")]
public IActionResult AdminOnly()
{
    return Ok("Admin access");
}

Password Rules & Security

Identity enforces strong defaults:

  • Minimum length
  • Uppercase
  • Numbers
  • Special characters
  • Lockout after failures

Customize rules:

builder.Services.Configure<IdentityOptions>(options =>
{
    options.Password.RequiredLength = 8;
    options.Lockout.MaxFailedAccessAttempts = 5;
});

Common Mistakes 🚫

  • Storing plain passwords
  • Bypassing Identity for login
  • Returning Identity entities directly
  • Disabling password policies
  • Not using email confirmation

Best Practices βœ…

  • Always use Identity for user management
  • Combine Identity with JWT for APIs
  • Use DTOs
  • Enable account lockout
  • Add email confirmation in production
  • Never expose sensitive fields

What You’ve Learned

βœ” What ASP.NET Core Identity is
βœ” Identity architecture
βœ” User registration & login
βœ” Password hashing
βœ” Roles & authorization
βœ” Identity + JWT integration

You now have enterprise-grade user management in your ASP.NET Core apps πŸ‘€πŸ”

πŸ”œ Coming Next Article

Refresh Tokens & Secure Session Management

You’ll learn:

  • Access vs Refresh tokens
  • Token rotation
  • Logout security
  • Preventing token theft
0 Comments