liveBook · Manning

Security requirement

As explained in Building Web APIs with ASP.NET Core

A security requirement is an API documentation configuration declaration specifying the security schemes required for an operation or API.

A security requirement tells Swagger which authorization scheme an API operation requires. A global requirement marks every endpoint as protected, so Swagger UI displays authorization indicators even for endpoints that do not require authorization. section 9.3.1 section 11.3.1

Configure bearer-token protection

Register a Bearer security definition with the Authorization header, HTTP bearer scheme, and JWT format, then attach an OpenAPI security requirement referencing Bearer. Swagger UI consequently provides an Authorize button for entering a token. section 9.3.1

For accurate documentation, replace the global requirement with an operation filter. The filter checks endpoint metadata for AuthorizeAttribute, leaves unrestricted operations unchanged, and assigns operation.Security only to restricted operations.

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines global and operation-specific requirements, then descends into an IOperationFilter implementation.
Examples
Demonstrates Program.cs bearer configuration, swagger.json security data, and an AuthRequirementFilter implementation.
What it assumes
Assumes familiarity with API authorization configuration and endpoint metadata while demonstrating the required Swagger setup.
What you can do after
Operate/configure Swagger UI to enter bearer tokens and display padlock icons only for authorization-restricted endpoints.
How it connects
Connects OpenAPI, Swagger UI, bearer tokens, JWT, AuthorizeAttribute, and IOperationFilter.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.