dotnet / dotnet/aspnetcore

JWT token validation ignores ValidAudiences from config in some scenarios

Open
#63,891 0 comments 0 reactions 0 assignees View on GitHub
area-auth
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

https://github.com/dotnet/aspnetcore/blob/0585ae7d9f9361bed031ce01e4a2f6eeab7438c4/src/Security/Authentication/OpenIdConnect/src/OpenIdConnectPostConfigureOptions.cs#L63

When using the app.settings configuration for JWT, e.g.
```json
"Authentication": {
"Schemes": {
"Bearer": {
"ValidAudiences": [
"my audience"
],
"ValidIssuer": "dotnet-user-jwts"
},
```

- scenario 1: calling AddJwtBearer without options
```
builder.Services.AddAuthentication()
.AddJwtBearer()
```
result: the JWT token validation is successful.

- scenario 2: calling AddJwtBearer with TokenValidationParameters options
```
builder.Services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
});

```
result: the JWT token validation fails
**WWW-Authenticate: Bearer error="invalid_token", error_description="The audience 'my audience' is invalid"**

- scenario 3: calling AddJwtBearer with TokenValidationParameters options and specifying Audience
```
builder.Services.AddAuthentication()
.AddJwtBearer(options =>
{
options.Audience = "my audience";
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
});

```
result: the JWT token validation is successful.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.