liveBook · Manning

HTTP Authorization header

As explained in Building Web APIs with ASP.NET Core

The Authorization header is an HTTP request header that carries credentials or authentication information for the requested resource.

The Authorization header carries credentials or authentication information for a requested resource. With bearer-token authorization, a client places a previously issued token in this header on later requests, avoiding repeated authentication. section 9.1.2

For the board-game API, the login endpoint returns a JWT. SwaggerUI can accept that token through an Authorize dialog and attach it to requests before they are sent. section 9.3.1

Configure and verify bearer-token requests by following these steps:

  • Add a Bearer security definition in Program.cs with the Authorization header, HTTP bearer scheme, and JWT bearer format. section 9.3.1
  • Add a global security requirement so SwaggerUI applies the declared protection to API requests. section 9.3.1
  • Click SwaggerUI’s Authorize button, enter the token returned by login, and perform a request to confirm that the header is attached. section 9.3.1
Listing 9.8 Program.cs file: Swagger’s bearer tokens setup
using Microsoft.OpenApi.Models;                                      #1
 
// ... existing code
 
builder.Services.AddSwaggerGen(options => {
    options.ParameterFilter<SortColumnFilter>();
    options.ParameterFilter<SortOrderFilter>();
 
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme #2
{
In = ParameterLocation.Header,
Description = "Please enter token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
 
options.AddSecurityRequirement(new OpenApiSecurityRequirement    #3
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
Array.Empty<string>()
}
});
});

Inspect the concrete Program.cs configuration that defines the Authorization header and enforces bearer-token security in SwaggerUI. section 9.3.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains bearer-token use and SwaggerUI configuration, but stops short of interceptor implementation and server-side token validation.
Examples
Uses a login-returned JWT, SwaggerUI’s Authorize dialog, and a Program.cs security definition as concrete artifacts.
What it assumes
Assumes REST statelessness and a login endpoint that returns a JWT; teaches the header’s bearer-token role directly.
What you can do after
Operate/configure SwaggerUI to collect a JWT and attach it through the Authorization header on API requests.
How it connects
Connects authentication, authorization, bearer tokens, JWT, SwaggerUI, REST, and HTTP interceptors.

Also explained in

Compare how 2 books explain HTTP Authorization header →

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.