ASP.NET Core

Adding a JWT Authorize button to Swagger UI

DEV Community: #aspnetcore Softinbit
ASP.NET Core

Swagger UI leaves JWT-protected endpoints without an Authorize button until the OpenAPI document declares a bearer scheme. The guide wires that scheme plus an operation filter for Swashbuckle 6-10 and the built-in .NET 9/10 OpenAPI.

Key points

  • A securitySchemes entry of type http with scheme bearer is what makes Swagger UI render the Authorize button.
  • Paste the raw access token; the UI adds the Bearer prefix and sends Authorization: Bearer <token> itself.
  • An operation filter reads IAuthorizeData and IAllowAnonymous, so login stays unlocked and only protected endpoints get a lock.
  • Swashbuckle 10 moved to Microsoft.OpenApi 2.x: types leave Microsoft.OpenApi.Models and OpenApiSecuritySchemeReference replaces Reference.
  • AddSecurityRequirement now takes a Func<OpenApiDocument, OpenApiSecurityRequirement>, which breaks older tutorial snippets.
  • Mixing Microsoft.AspNetCore.OpenApi 8.x/9.x (Microsoft.OpenApi 1.x) with 10.x turns the first document request into a 500.

Why it matters

Anyone shipping a JWT-protected API has hit this: the token works in Postman, the Swagger page returns 401, and the fix looks like a middleware problem when it is only a missing document entry. What changed lately is the packaging. Swashbuckle 10 and the built-in OpenAPI in .NET 10 both sit on Microsoft.OpenApi 2.x, so older snippets no longer compile, and a mismatch between Microsoft.AspNetCore.OpenApi and Swashbuckle surfaces at the first document request rather than at build time. Check your transitive Microsoft.OpenApi version before copying a tutorial, and keep the UI registration inside the Development branch.

Real-world use

  • A Web API where POST /api/auth/login must stay anonymous while controller-level [Authorize] endpoints show the lock.
  • A .NET 9 or 10 service using the built-in AddOpenApi() output with Scalar instead of Swashbuckle.
  • A team that wants to paste a token once and still call protected endpoints from the browser after a page reload.

This summary was generated by the agent from the original post on DEV Community: #aspnetcore. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message