liveBook · Manning

Authorization decision

As explained in Building Web APIs with ASP.NET Core

Authorization is the process of determining whether an authenticated or anonymous caller is permitted to access a resource or perform an operation.

An authorization decision answers a practical question: may this caller perform this action on this resource? Authentication establishes who the caller is; authorization determines what that caller may do. Permissions can allow or deny actions such as reading, updating, and deleting. Anonymous callers typically receive only the permissions needed to reach public content or a login endpoint. section 9.1.2 section 9.6

Consider a board-game API with update and delete endpoints. Leaving those endpoints open lets anyone alter its data. An initial restriction can require an authenticated caller, but that makes every signed-in user eligible. A more precise decision also checks the caller’s assigned role. section 9.0 section 9.3.3

To make the decisions more specific:

  • Require the Moderator role on update methods in BoardGamesController, DomainsController, and MechanicsController. section 9.4.5
  • Require the Administrator role on delete methods and SeedController. section 9.4.5
  • Include each assigned role as a claim in the user’s JSON Web Token (JWT), then use `[Authorize(Roles = ...)]` to check the required role. section 9.4.5
app.MapGet("/auth/test/2",
   [Authorize(Roles = RoleNames.Moderator)]
   [EnableCors("AnyOrigin")]
   [ResponseCache(NoStore = true)] () =>
   {
       return Results.Ok("You are authorized!");
   });

app.MapGet("/auth/test/3",
   [Authorize(Roles = RoleNames.Administrator)]
   [EnableCors("AnyOrigin")]
   [ResponseCache(NoStore = true)] () =>
   {
       return Results.Ok("You are authorized!");
   });

These harmless Minimal API endpoints separate the Moderator and Administrator checks from operations that would permanently change data. section 9.4.6

Check the decision without changing data

Call a protected test endpoint in SwaggerUI before logging in: the expected response is 401 Unauthorized. Log in, supply the resulting JWT through SwaggerUI’s Authorize button, and try again before the token expires; the authenticated-only test returns 200 OK. For role-restricted tests, a user with no role cannot access either endpoint, a Moderator can access the Moderator endpoint, and an account with both roles can access both. These checks show why a valid login alone is not enough for every operation. section 9.3.3 section 9.4.6

Role-based access control is one option in ASP.NET Core Identity. Claim-based checks can examine other claims, while policy-based checks support more advanced requirements. Whichever rule you choose, protect bearer tokens from third-party access and make endpoint authorization requirements clear in API documentation. section 9.6 section 11.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Works through authenticated-only and role-based checks with endpoint tests, while only summarizing claim-based and policy-based alternatives.
Examples
Tests protected Minimal API endpoints in SwaggerUI using accounts with no role, Moderator, or both Moderator and Administrator roles.
What it assumes
Defines authentication and authorization before applying them to an existing API with controllers and endpoints.
What you can do after
Apply it via ASP.NET Core’s Authorize attribute and verify authenticated-only and role-restricted endpoints with JWTs in SwaggerUI.
How it connects
Connects authorization to authentication, JWT role claims, role-based access control, and access policies.

Also explained in

Compare how 7 books explain authorization decision →

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.