liveBook · Manning

AuthorizeAsync

As explained in ASP.NET Core in Action, 3e

AuthorizeAsync is an ASP.NET Core authorization API method that asynchronously evaluates whether a user satisfies an authorization requirement or policy.

AuthorizeAsync asynchronously checks whether a user may perform an action. It receives the ClaimsPrincipal, the resource being protected, and the policy to evaluate, then returns an AuthorizationResult. section 24.5.1

Use the result to control the request or the user interface:

  • Check Succeeded before continuing. A failed server-side check should return ForbidResult, producing a 403 response or an access-denied redirect. section 24.5.1
  • Pass the actual resource, such as a Recipe, when authorization depends on ownership or other resource data. section 24.5.1
  • Use the result to hide an Edit link, but retain the server-side check because UI hiding does not prevent direct access. section 24.6
  • Reuse the same authorization handlers from Razor Pages in minimal API endpoints, while considering filters or domain-level checks when endpoint logic is duplicated. section 25.6

Protecting a recipe deletion endpoint

Fetch the recipe, call AuthorizeAsync with the request user, recipe, and CanManageRecipe policy, return Results.Forbid() when Succeeded is false, and delete only after authorization succeeds. section 25.6

Listing 24.14 Using IAuthorizationService for resource-based authorization
[Authorize]                                                       #1
public class EditModel : PageModel
{
    [BindProperty]
    public Recipe Recipe { get; set; }
 
    private readonly RecipeService _service;
    private readonly IAuthorizationService _authService;          #2
 
    public EditModel(
        RecipeService service,
        IAuthorizationService authService)                        #2
    {
        _service = service;
        _authService = authService;                               #2
    }
 
    public async Task<IActionResult> OnGet(int id)
    {
        Recipe = _service.GetRecipe(id);                          #3
        AuthorizationResult authResult = await _authService       #4
            .AuthorizeAsync(User, Recipe, "CanManageRecipe");     #4
        if (!authResult.Succeeded)                                #5
        {                                                         #5
            return new ForbidResult();                            #5
        }                                                         #5
 
        return Page();                                            #6
    }
}

Follow a complete Razor Page example showing dependency injection, resource loading, AuthorizeAsync, and failed-request handling. section 24.5.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains inputs, result handling, policy dependencies, UI use, and endpoint placement without detailing handler internals.
Examples
Uses recipe editing, conditional Edit links, and recipe deletion as resource-authorization examples.
What it assumes
Assumes dependency injection, ClaimsPrincipal users, authorization policies, and resources such as Recipe objects.
What you can do after
Apply it via IAuthorizationService to protect actions, return forbidden responses, and conditionally render authorized controls.
How it connects
Connects ClaimsPrincipal, authorization policies, requirements, handlers, resources, Razor Pages, and minimal APIs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.