AspNetCore.Docs/aspnetcore/security/authorization/resourcebased.md

7.3 KiB

title author description ms.author ms.custom ms.date uid
Resource-based authorization in ASP.NET Core scottaddie Learn how to implement resource-based authorization in an ASP.NET Core app when an Authorize attribute won't suffice. scaddie mvc 11/07/2017 security/authorization/resourcebased

Resource-based authorization in ASP.NET Core

Authorization strategy depends upon the resource being accessed. Consider a document which has an author property. Only the author is allowed to update the document. Consequently, the document must be retrieved from the data store before authorization evaluation can occur.

Attribute evaluation occurs before data binding and before execution of the page handler or action which loads the document. For these reasons, declarative authorization with an [Authorize] attribute won't suffice. Instead, you can invoke a custom authorization method—a style known as imperative authorization.

Use the sample apps (how to download) to explore the features described in this topic.

Create an ASP.NET Core app with user data protected by authorization contains a sample app that uses resource-based authorization.

Use imperative authorization

Authorization is implemented as an IAuthorizationService service and is registered in the service collection within the Startup class. The service is made available via dependency injection to page handlers or actions.

[!code-csharp]

IAuthorizationService has two AuthorizeAsync method overloads: one accepting the resource and the policy name and the other accepting the resource and a list of requirements to evaluate.

ASP.NET Core 2.x

Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user,
                          object resource,
                          IEnumerable<IAuthorizationRequirement> requirements);
Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user,
                          object resource,
                          string policyName);

ASP.NET Core 1.x

Task<bool> AuthorizeAsync(ClaimsPrincipal user,
                          object resource,
                          IEnumerable<IAuthorizationRequirement> requirements);
Task<bool> AuthorizeAsync(ClaimsPrincipal user,
                          object resource,
                          string policyName);

In the following example, the resource to be secured is loaded into a custom Document object. An AuthorizeAsync overload is invoked to determine whether the current user is allowed to edit the provided document. A custom "EditPolicy" authorization policy is factored into the decision. See Custom policy-based authorization for more on creating authorization policies.

[!NOTE] The following code samples assume authentication has run and set the User property.

ASP.NET Core 2.x

[!code-csharp]

ASP.NET Core 1.x

[!code-csharp]


Write a resource-based handler

Writing a handler for resource-based authorization isn't much different than writing a plain requirements handler. Create a custom requirement class, and implement a requirement handler class. The handler class specifies both the requirement and resource type. For example, a handler utilizing a SameAuthorRequirement requirement and a Document resource looks as follows:

ASP.NET Core 2.x

[!code-csharp]

ASP.NET Core 1.x

[!code-csharp]


Register the requirement and handler in the Startup.ConfigureServices method:

[!code-csharp]

Operational requirements

If you're making decisions based on the outcomes of CRUD (Create, Read, Update, Delete) operations, use the OperationAuthorizationRequirement helper class. This class enables you to write a single handler instead of an individual class for each operation type. To use it, provide some operation names:

[!code-csharp]

The handler is implemented as follows, using an OperationAuthorizationRequirement requirement and a Document resource:

ASP.NET Core 2.x

[!code-csharp]

ASP.NET Core 1.x

[!code-csharp]


The preceding handler validates the operation using the resource, the user's identity, and the requirement's Name property.

To call an operational resource handler, specify the operation when invoking AuthorizeAsync in your page handler or action. The following example determines whether the authenticated user is permitted to view the provided document.

[!NOTE] The following code samples assume authentication has run and set the User property.

ASP.NET Core 2.x

[!code-csharp]

If authorization succeeds, the page for viewing the document is returned. If authorization fails but the user is authenticated, returning ForbidResult informs any authentication middleware that authorization failed. A ChallengeResult is returned when authentication must be performed. For interactive browser clients, it may be appropriate to redirect the user to a login page.

ASP.NET Core 1.x

[!code-csharp]

If authorization succeeds, the view for the document is returned. If authorization fails, returning ChallengeResult informs any authentication middleware that authorization failed, and the middleware can take the appropriate response. An appropriate response could be returning a 401 or 403 status code. For interactive browser clients, it could mean redirecting the user to a login page.