.NET › Authentication & Authorization (Identity, JWT, OAuth)
How does JWT (JSON Web Token) authentication work at a high level in ASP.NET Core?
IntermediateAnswer
A client authenticates once (e.g., with credentials) and receives a signed JWT containing claims about the user (identity, roles, expiration) encoded in its payload. For subsequent requests, the client sends this token (typically in the Authorization: Bearer header), and JWT bearer middleware validates the token's signature, expiration, issuer, and audience without needing a server-side session lookup, making it stateless and well-suited for APIs and distributed systems.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = true,
ValidIssuer = "myapp",
ValidateAudience = true,
ValidAudience = "myapp-api",
IssuerSigningKey = new SymmetricSecurityKey(key)
};
});Real-world example A single-page application logs in once, receives a JWT, and includes it as a Bearer token on every subsequent API call, letting the stateless API validate each request independently without a shared session store.