JWT Authentication in ASP.NET Core
๐ Introduction
Security is one of the most important parts of any application. Today, most modern apps use token-based authentication instead of session-based login.
๐ One of the most popular methods is JWT (JSON Web Token) in ASP.NET Core.
๐ค What is JWT?
JWT (JSON Web Token) is a secure token that is generated after login and used to access protected APIs.
๐ Instead of storing user session on server, JWT stores data in token.
๐ง Simple Flow (Easy Understanding)
User Login
Server verifies user
Server generates JWT token
Client stores token
Client sends token in every request
Server validates token
๐ฅ Why JWT is Trending?
โ Stateless (No session needed)
โ Secure
โ Fast
โ Used in Mobile + Web APIs
โ Industry standard
๐๏ธ Step 1: Create Web API Project
dotnet new webapi -n JwtAuthDemo๐ฆ Step 2: Install Required Package
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearerโ๏ธ Step 3: Configure JWT in Program.cs
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
var key = "ThisIsMySecretKey12345";
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "JWT API Running");
app.Run();๐ Step 4: Create Token Generator
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
public class JwtService
{
private string key = "ThisIsMySecretKey12345";
public string GenerateToken(string username)
{
var claims = new[]
{
new Claim(ClaimTypes.Name, username)
};
var keyBytes = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var creds = new SigningCredentials(keyBytes, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds
);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}๐ Step 5: Login API (Generate Token)
app.MapPost("/login", (string username, string password) =>
{
if (username == "admin" && password == "123")
{
var jwt = new JwtService();
var token = jwt.GenerateToken(username);
return Results.Ok(token);
}
return Results.Unauthorized();
});๐ Step 6: Secure API
app.MapGet("/secure", () =>
{
return "This is protected data";
}).RequireAuthorization();๐ก How to Use in Postman
Call /login โ get token
Copy token
Go to Headers
Add:
Authorization: Bearer YOUR_TOKENCall /secure
๐ง Easy Understanding
Token = Identity Card ๐ชช
Without token โ access denied
With token โ access allowed
๐ Real-Life Use Cases
Mobile apps login
Banking APIs
E-commerce systems
Microservices authentication
๐ Conclusion
JWT authentication in ASP.NET Core is:
๐ Secure
โก Fast
๐ Widely used

Join the conversation! Your thoughts help the community grow.