JSON Web Tokens (JWT) are one of the most popular ways to secure APIs. When combined with ASP.NET Core Identity and Entity Framework Core (EF), they provide a powerful and flexible authentication system for modern web applications.
In this article, you’ll learn how to:
Set up ASP.NET Core Identity with EF Core
Configure JWT token authentication
Generate and validate JWTs in an API
Secure your endpoints with
[Authorize]
🧩 What is JWT (JSON Web Token)?
A JWT is a compact, URL-safe token used to securely transmit information between parties.
It consists of three parts:
header.payload.signature
Example JWT
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiJ2aXBpbi5taXR0YWxAZ21haWwuY29tIiwiaWF0IjoxNjk2MDE0MzQ3LCJleHAiOjE2OTYwMTc5NDd9.
3nwbOScm9vPh3O4uH0MknhrK5hLbAwT7LM0I9_rM9w4
Each part represents:
Header → algorithm & token type
Payload → user data (claims)
Signature → verifies token integrity
📘 Reference:
JWT.io — Introduction to JWT
⚙️ Step 1. Create a New ASP.NET Core Web API Project
dotnet new webapi -n JwtAuthApi
cd JwtAuthApi
🧱 Step 2. Add Required NuGet Packages
Run these commands:
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
🗃️ Step 3. Configure ApplicationDbContext
Create a new folder Data → ApplicationDbContext.cs :
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace JwtAuthApi.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) { }
}
}
👤 Step 4. Register Identity and JWT in Program.cs
using JwtAuthApi.Data;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// 1️⃣ Configure EF Core
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// 2️⃣ Add Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// 3️⃣ Configure JWT Authentication
var jwtKey = builder.Configuration["Jwt:Key"];
var jwtIssuer = builder.Configuration["Jwt:Issuer"];
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtIssuer,
ValidAudience = jwtIssuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
};
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

Join the conversation! Your thoughts help the community grow.