dotnet / dotnet/aspnetcore

Setup JwtBearer authentication with faulty authority should crash

Open
#67,991 10 comments 1 reaction 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

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I misconfigured my multi tenant application to use the `https://login.microsoftonline.com/organisations/v2.0` url for the Authority.

This does not return a valid openid configuration (returns a 400, not a 404 but still not a success request). In this case I would expect the application to not even start, as misconfiguring something like this is a big issue.

### Expected Behavior

My expectations is that either the app would not start or at the first time it would use the authentication handler it would write a fatal error to the logs. But instead of that nothing!!!

It seems to continue working. Because I set options.TokenValidationParameters.ValidateIssuerSigningKey to true it comes in the OnAuthenticationFailed event, but that is only because I explicitly set it to validate tokens.

### Steps To Reproduce

1. Create a new web api project without authentication.
2. Add the reference ``
3. Configure out as follows
4. Access any endpoint

```csharp
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = new Uri("https://login.microsoftonline.com/organisations/v2.0");

// Block insecure HTTP metadata and enforce HTTPS for token validation
options.RequireHttpsMetadata = true;
// Override specific options if needed
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidateIssuer = false; // Set to false to allow tokens from multiple tenants
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ClockSkew = TimeSpan.FromSeconds(15);
options.TokenValidationParameters.RequireSignedTokens = true;
options.TokenValidationParameters.RequireExpirationTime = true;

options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = (ctx) =>
{
// Set breakpoint here to see the actual error (No signing keys provided)
return Task.CompletedTask;
},
OnChallenge = (ctx) =>
{
return Task.CompletedTask;
}
};

});

```

### Exceptions (if any)

_No response_

### .NET Version

10.0.10

### Anything else?

It not crashing but silently continuing might lead to developers unknowingly not securing their apis. Since this is a misconfiguration I would expect it to be a little bit more verbose about that. Either a fatal error in the logging or a crash.

Contributor guide

Open the contributing guide

Research direction

Start at the AddJwtBearer configuration entry point and reproduce the faulty Authority setup by accessing an endpoint. Trace how OpenID configuration failures are handled and add a regression test covering the non-success metadata response; done means the misconfiguration produces the expected startup or authentication failure and is visible in logs.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, authentication, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.