liveBook · Manning

Security scheme

As explained in ASP.NET Core in Action, 3e

An OpenAPI security scheme is a declaration describing the authentication or authorization mechanism an HTTP API uses to protect operations.

An OpenAPI security scheme describes how an HTTP API authenticates or authorizes callers. Defining the scheme lets Swagger UI understand the protection mechanism and provide an authorization workflow for testing protected operations. section 25.5

Configure a scheme in two steps:

  • Define the mechanism, such as JWT bearer authentication, OAuth 2.0, OpenID Connect, or simple bearer authentication. section 25.5
  • Declare which endpoints use the scheme by adding a security requirement; applying it globally protects the whole API. section 25.5 section 25.7
  • Register an OpenApiSecurityScheme with Swashbuckle using AddSecurityDefinition(), then connect it with AddSecurityRequirement(). section 25.7

JWT bearer authentication in Swagger UI

After registering a bearer scheme, Swagger UI displays an Authorize button. Enter Bearer <token>, including the space and prefix; Swagger UI then saves the value and attaches it to subsequent requests in the Authorization header. section 25.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines schemes and requirements with Swashbuckle, but stops short of implementing authentication or authorization itself.
Examples
Uses an OpenApiSecurityScheme, AddSecurityDefinition(), AddSecurityRequirement(), and Swagger UI's Bearer token authorization dialog.
What it assumes
Assumes an API authentication mechanism and token are available; teaches the OpenAPI definition and requirement needed to expose them in Swagger UI.
What you can do after
Configure it via Swashbuckle so Swagger UI sends bearer tokens in Authorization headers when testing protected API operations.
How it connects
Links OpenAPI with Swagger UI, Swashbuckle, JWT bearer authentication, OAuth 2.0, OpenID Connect, and authorization.

Also explained in

Compare how 3 books explain security scheme →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.