Skip to content

JWT Authentication Events Return Incorrect HTTP Status Codes #30918

Closed
@tymur999

Description

@tymur999

The JwtBearerOptions.Events when .AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(...) is injected does not return the correct http status codes as specified. When adding these events to be called when the request is challenged or forbidden, the WriteAsJsonAsync method specifically describes that it will return a 200 HTTP status. This occurs normally with the OnChallenge handler, where a 200 is returned, but OnForbidden still returns a 403 even though the same method WriteAsJsonAsync says that it will return 200 HTTP status.

.AddJwtBearer(options =>
                {
                    options.Events = new JwtBearerEvents
                    {
                        OnChallenge = ctx =>
                            ctx.Response.WriteAsJsonAsync(<response object here>), //Returns 200, normal
                        OnForbidden = ctx =>
                            ctx.Response.WriteAsJsonAsync(<response object here>) //Returns 403, but supposed to be 200
                    };
                });

I know that 403 really means forbidden, and it says that is what is supposed to be returned for OnForbidden in MS Docs, but the WriteAsJsonAsync specifically says that

"the status code [will be] set to 200."

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Status: Resolvedarea-authIncludes: Authn, Authz, OAuth, OIDC, Bearer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions