liveBook · Manning

Chapter 24: Authorization: Securing your application

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

This chapter introduction frames authorization as deciding what authenticated users may do and previews the ASP.NET Core techniques used to enforce it.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction frames authorization as deciding what authenticated users may do and previews the ASP.NET Core techniques used to enforce it.

Key terms: authentication, authorization, authenticated users, claims-based authorization, policies, authorization requirements, authorization handlers, resource-based authorization, Razor Pages

24.1 Introduction to authorization

This section distinguishes authentication from authorization and uses an airport journey to introduce claims-based authorization.

Key terms: authentication, authorization, authenticated request, claim, type, value, BoardingPassNumber, FrequentFlyerClass, Gold, unauthorized request

24.2 Authorization in ASP.NET Core

This section explains how ASP.NET Core authorizes a routed, authenticated request through endpoint metadata and AuthorizationMiddleware before the endpoint executes.

Key terms: AuthorizationMiddleware, UseAuthorization(), UseRouting(), UseAuthentication(), UseEndpoints(), RoutingMiddleware, [Authorize], endpoint metadata, authorization requirements, HttpContext.User

24.2.1 Preventing anonymous users from accessing your application

This section shows how to restrict endpoints to authenticated users with [Authorize] and exempt necessary public endpoints with [AllowAnonymous].

Key terms: [Authorize], authenticated user, unauthenticated user, action, Razor Page, authorization metadata, AuthorizationMiddleware, RoutingMiddleware, endpoint, [AllowAnonymous]

24.2.2 Handling unauthorized requests

This section explains how ASP.NET Core distinguishes challenge and forbid authorization failures and produces different responses for cookie-based web apps and APIs.

Key terms: AuthorizationMiddleware, challenge, forbid, authorization requirements, claim, [Authorize], cookie authentication, login page, access denied, ASP.NET Core Identity

24.3 Using policies for claims-based authorization

This section shows how to define and apply named authorization policies that use user claims to control access to ASP.NET Core endpoints.

Key terms: ClaimsPrincipal, claim, claims-based authorization, policy, authorization, Authorize, authorization middleware, EndpointMiddleware, unauthenticated, forbidden

24.4 Creating custom policies for authorization

This section introduces why complex authorization rules require custom policy requirements and handlers rather than only the basic claim checks of AuthorizationPolicyBuilder.

Key terms: AuthorizationPolicyBuilder, claim, FrequentFlyerClass, DateOfBirth, EmployeeNumber, IsBannedFromLounge, custom authorization requirements, handlers, policy

24.4.1 Requirements and handlers: The building blocks of a policy

This section explains how authorization policies are composed from requirements and handlers and how their AND/OR evaluation controls access.

Key terms: policy, requirement, handler, CanAccessLounge, MinimumAgeRequirement, AllowedInLoungeRequirement, authorize middleware, protected endpoint, FrequentFlyerHandler, IsAirlineEmployeeHandler

24.4.2 Creating a policy with a custom requirement and handler

This section builds the CanAccessLounge policy from custom requirement objects and DI-registered handlers, explaining how handlers determine authorization success or failure.

Key terms: IAuthorizationRequirement, requirement, AllowedInLoungeRequirement, MinimumAgeRequirement, POCO, CanAccessLounge, AuthorizationPolicyBuilder, Authorize, authorization policy, Program.cs

24.5 Controlling access with resource-based authorization

This section introduces resource-based authorization for cases where access depends on details of a specific loaded resource, using recipe editing as the example.

Key terms: resource-based authorization, Recipe, resource, AuthorizeAttribute, [Authorize], Create.cshtml, Razor Page, page handlers, integer ID, insecure direct object reference

24.5.1 Manually authorizing requests with IAuthorizationService

This section shows how to perform imperative, resource-based authorization after loading a Recipe by calling IAuthorizationService from a Razor Page.

Key terms: declarative authorization, imperative authorization, [Authorize] attribute, resource-based authorization, Recipe, IAuthorizationService, AuthorizeAsync, ClaimsPrincipal, User, CanManageRecipe

24.5.2 Creating a resource-based AuthorizationHandler

This section shows how to implement and register a resource-based authorization handler, and weighs framework authorization against manual domain-level checks.

Key terms: AuthorizationHandler<TRequirement, TResource>, HandleRequirementAsync, requirement, resource, IAuthorizationService, IsRecipeOwnerHandler, IsRecipeOwnerRequirement, Recipe, ClaimsPrincipal, ApplicationUser

24.6 Hiding HTML elements from unauthorized users

This section improves Razor Pages user experience by hiding unauthorized UI elements while preserving server-side authorization as the essential security control.

Key terms: server-side authorization, Edit.cshtml page handler, client-side security, IAuthorizationService, AuthorizeAsync, CanEditRecipe, PageModel, View.cshtml, Razor template, anonymous users

24.7 Summary

This summary recaps how ASP.NET Core authenticates users, authorizes their access through attributes and policies, and enforces more complex or resource-specific rules on the server.

Where this is used later

Concepts explained in this chapter

← Chapter 23: Authentication: Adding users to your application with Identity Chapter 25: Authentication and authorization for APIs →

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.