UseAuthorization() adds authorization middleware to an ASP.NET Core request pipeline. It checks the selected endpoint’s authorization requirements and uses HttpContext.User to decide whether the request may continue. section 24.2
Place the middleware in the pipeline in this order:
- Call UseRouting() first so the selected endpoint and its authorization metadata are available. section 24.2
- Call UseAuthentication() before UseAuthorization() so the current user is available. section 24.2
- Call UseAuthorization() before UseEndpoints() so authorization runs before the endpoint executes. section 24.2
- Remember that WebApplication normally adds this middleware in the correct order; verify the order when customizing the pipeline. section 24.2
Protected endpoint flow
A request reaches UseAuthorization() after routing and authentication. If the endpoint has requirements, the middleware checks the authenticated user. An authorized request continues; an unauthorized request stops before the endpoint middleware runs. section 24.2
Figure 24.2 Authorization occurs after an endpoint has been selected and after the request is authenticated, but before the action method or Razor Page endpoint is executed.
Use the pipeline diagram to visualize authorization after endpoint selection and authentication, but before the action method or Razor Page endpoint executes. section 24.2
