liveBook · Manning

Chapter 25: Authentication and authorization for APIs

What you will learn in chapter 25 of ASP.NET Core in Action, 3e

This chapter introduces how API authentication differs from cookie-based web-app authentication and previews JWT-based authentication, testing, OpenAPI integration, and authorization for minimal APIs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces how API authentication differs from cookie-based web-app authentication and previews JWT-based authentication, testing, OpenAPI integration, and authorization for minimal APIs.

Key terms: encrypted cookies, AuthenticationMiddleware, API applications, traditional web apps, OAuth 2.0, OpenID Connect, JSON Web Tokens, JWTs, JWT bearer authentication, .NET CLI

25.1 Authentication for APIs and distributed applications

Introduces why API authentication differs from traditional web-app authentication and previews common distributed-application patterns and protocols.

Key terms: authentication, API applications, traditional web apps, distributed applications

25.1.1 Extending authentication to multiple apps

This section explains why cookie-based authentication suits a single traditional web app but becomes difficult to manage when one user interacts with multiple apps and APIs.

Key terms: encrypted cookie, ClaimsPrincipal, claims, AuthenticationMiddleware, traditional web app, authentication, user management, business logic, mobile applications, single-page applications

25.1.2 Centralizing authentication in an identity provider

This section explains how a centralized identity provider authenticates users for multiple applications and APIs, why this architecture is valuable, and the trade-offs between third-party and self-built providers.

Key terms: identity provider, bearer tokens, client, APIs, authentication, authorization, JWTs, signed, tampering, authentication details

25.1.3 OpenID Connect and OAuth 2.0

This section distinguishes OAuth 2.0 authorization from OpenID Connect authentication, illustrating their flows, common grant types, and relevance to API builders.

Key terms: OAuth 2.0, authorization protocol, delegated access, protected resource, identity provider, authorization code, access token, consent screen, secret, Facebook API

25.2 Understanding bearer token authentication

This section explains bearer-token authentication for APIs, focusing on JWT structure, token types and trade-offs, and how ASP.NET Core validates a JWT into an authenticated user.

Key terms: bearer token, security token, protected resource, JWT, JSON Web Token, header, payload, claims, signature, sub

25.3 Adding JWT bearer authentication to minimal APIs

This section shows how to add JWT bearer authentication to a minimal API, protect an endpoint with authorization, and understand how authentication schemes select cookie or bearer-token behavior.

Key terms: Microsoft.AspNetCore.Authentication.JwtBearer, JWT bearer authentication, JWT bearer authentication scheme, minimal API, RequireAuthorization(), authorization policy, Is authenticated, [Authorize], 401 Unauthorized, authorization middleware

25.4 Using the user-jwts tool for local JWT testing

This section introduces the built-in user-jwts .NET CLI tool as a lightweight local identity provider for generating JWTs to test protected APIs.

Key terms: JWT, JWT authentication, authorization policy, identity provider, OpenID Connect, user-jwts, .NET CLI, mini identity provider, claims, signing key material

25.4.1 Creating JWTs with the user-jwts tool

This section shows how to generate a development JWT with user-jwts and send it as a bearer token when testing a local API.

Key terms: user-jwts, dotnet user-jwts create, User Secrets, signing key material, appsettings.Development.json, JWT bearer authentication, sub, unique_claim, aud, iss

25.4.2 Customizing your JWTs

This section shows how to customize locally generated JWT claims and choose command output formats that suit interactive use or scripts.

Key terms: dotnet user-jwts create, --name, sub claim, unique_name claim, --claim, --scope, --output, default, token, json

25.4.3 Managing your local JWTs

This section explains where user-jwts stores local JWT configuration, how to manage those tokens safely, and why they are limited to development testing.

Key terms: user-jwts.json, User Secrets, <UserSecretsId>, appsettings.Development.json, appsettings.json, identity provider, dotnet user-jwts, list, clear, remove

25.5 Describing your authentication requirements to OpenAPI

This section shows how to describe API authentication in OpenAPI so Swagger UI can send JWT bearer tokens when testing protected endpoints.

Key terms: OpenAPI document, security scheme, OpenApiSecurityScheme, AddSecurityDefinition(), AddSecurityRequirement(), JWT bearer authentication, Swagger UI, Authorize, Bearer <token>, Authorization header

25.6 Applying authorization policies to minimal API endpoints

This section explains how to protect minimal API endpoints with declarative policies, resource-based authorization, and scope-based policies.

Key terms: RequireAuthorization(), [Authorize], policy, route group, minimal API endpoint, AllowAnonymous(), [AllowAnonymous], authentication requirements, resource-based authorization, AuthorizationHandler<TRequirement, TResource>

25.7 Summary

This summary recaps centralized identity, JWT bearer authentication and testing, and how to document and enforce API authorization in ASP.NET Core.

Where this is used later

Concepts explained in this chapter

← Chapter 24: Authorization: Securing your application Chapter 26: Monitoring and troubleshooting errors with logging →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.