Skip to content

Commit bb339ea

Browse files
pranavkmscottaddie
andauthored
Add some notes accessing controller action descriptor from auth handlers (#17829)
* Add some notes accessing controller action descriptor + auth handlers * Update policies.md * Edit pass on policy-based authz doc (#17833) Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com>
1 parent 9c0e83a commit bb339ea

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

aspnetcore/security/authorization/policies.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: rick-anderson
44
description: Learn how to create and use authorization policy handlers for enforcing authorization requirements in an ASP.NET Core app.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 10/05/2019
7+
ms.date: 04/15/2020
88
uid: security/authorization/policies
99
---
1010
# Policy-based authorization in ASP.NET Core
@@ -426,9 +426,19 @@ For example, the previous `BadgeEntryHandler` could be rewritten as follows:
426426

427427
## Accessing MVC request context in handlers
428428

429-
The `HandleRequirementAsync` method you implement in an authorization handler has two parameters: an `AuthorizationHandlerContext` and the `TRequirement` you are handling. Frameworks such as MVC or Jabbr are free to add any object to the `Resource` property on the `AuthorizationHandlerContext` to pass extra information.
429+
The `HandleRequirementAsync` method you implement in an authorization handler has two parameters: an `AuthorizationHandlerContext` and the `TRequirement` you are handling. Frameworks such as MVC or SignalR are free to add any object to the `Resource` property on the `AuthorizationHandlerContext` to pass extra information.
430430

431-
For example, MVC passes an instance of [AuthorizationFilterContext](/dotnet/api/?term=AuthorizationFilterContext) in the `Resource` property. This property provides access to `HttpContext`, `RouteData`, and everything else provided by MVC and Razor Pages.
431+
When using endpoint routing, authorization is typically handled by the Authorization Middleware. In this case, the `Resource` property is an instance of <xref:Microsoft.AspNetCore.Http.Endpoint>. The endpoint can be used to probe the underlying the resource to which you're routing. For example:
432+
433+
```csharp
434+
if (context.Resource is Endpoint endpoint)
435+
{
436+
var actionDescriptor = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
437+
...
438+
}
439+
```
440+
441+
With traditional routing, or when authorization happens as part of MVC's authorization filter, the value of `Resource` is an <xref:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext> instance. This property provides access to `HttpContext`, `RouteData`, and everything else provided by MVC and Razor Pages.
432442

433443
The use of the `Resource` property is framework specific. Using information in the `Resource` property limits your authorization policies to particular frameworks. You should cast the `Resource` property using the `is` keyword, and then confirm the cast has succeeded to ensure your code doesn't crash with an `InvalidCastException` when run on other frameworks:
434444

@@ -441,4 +451,4 @@ if (context.Resource is AuthorizationFilterContext mvcContext)
441451
}
442452
```
443453

444-
::: moniker-end
454+
::: moniker-end

0 commit comments

Comments
 (0)