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

130 lines
7.3 KiB
Markdown
Raw Normal View History

2016-10-29 01:35:15 +08:00
---
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
title: Resource-based authorization in ASP.NET Core
author: scottaddie
description: Learn how to implement resource-based authorization in an ASP.NET Core app when an Authorize attribute won't suffice.
2016-10-29 01:35:15 +08:00
manager: wpickett
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
ms.author: scaddie
ms.custom: mvc
ms.date: 11/07/2017
ms.devlang: csharp
ms.prod: asp.net-core
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
ms.technology: aspnet
ms.topic: article
2016-10-29 01:35:15 +08:00
uid: security/authorization/resourcebased
---
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
# Resource-based authorization
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
By [Scott Addie](https://twitter.com/Scott_Addie)
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
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.
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
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.
2016-10-29 01:35:15 +08:00
Update live with current master (#4755) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md * Fix broken sample apps link
2017-11-10 02:39:44 +08:00
Use the [sample apps](https://github.com/aspnet/Docs/tree/master/aspnetcore/security/authorization/resourcebased/samples) ([how to download](xref:tutorials/index#how-to-download-a-sample)) to explore the features described in this topic.
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
## Use imperative authorization
Authorization is implemented as an [IAuthorizationService](/dotnet/api/microsoft.aspnetcore.authorization.iauthorizationservice) service and is registered in the service collection within the `Startup` class. The service is made available via [dependency injection](xref:fundamentals/dependency-injection#fundamentals-dependency-injection) to page handlers or actions.
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Controllers/DocumentController.cs?name=snippet_IAuthServiceDI&highlight=6)]
`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.
2016-10-29 01:35:15 +08:00
# [ASP.NET Core 2.x](#tab/aspnetcore2x)
```csharp
Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user,
object resource,
IEnumerable<IAuthorizationRequirement> requirements);
Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user,
object resource,
string policyName);
```
# [ASP.NET Core 1.x](#tab/aspnetcore1x)
2016-11-18 13:03:07 +08:00
```csharp
2016-10-29 01:35:15 +08:00
Task<bool> AuthorizeAsync(ClaimsPrincipal user,
object resource,
IEnumerable<IAuthorizationRequirement> requirements);
Task<bool> AuthorizeAsync(ClaimsPrincipal user,
object resource,
string policyName);
```
2016-10-29 01:35:15 +08:00
---
2017-10-14 04:50:30 +08:00
<a name="security-authorization-resource-based-imperative"></a>
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
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](xref:security/authorization/policies) for more on creating authorization policies.
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
> [!NOTE]
> The following code samples assume authentication has run and set the `User` property.
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
# [ASP.NET Core 2.x](#tab/aspnetcore2x)
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Pages/Document/Edit.cshtml.cs?name=snippet_DocumentEditHandler)]
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
# [ASP.NET Core 1.x](#tab/aspnetcore1x)
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp1/Controllers/DocumentController.cs?name=snippet_DocumentEditAction)]
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
---
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
## Write a resource-based handler
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
Writing a handler for resource-based authorization isn't much different than [writing a plain requirements handler](xref:security/authorization/policies#security-authorization-policies-based-authorization-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:
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
# [ASP.NET Core 2.x](#tab/aspnetcore2x)
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Services/DocumentAuthorizationHandler.cs?name=snippet_HandlerAndRequirement)]
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
# [ASP.NET Core 1.x](#tab/aspnetcore1x)
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp1/Services/DocumentAuthorizationHandler.cs?name=snippet_HandlerAndRequirement)]
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
---
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
Register the requirement and handler in the `Startup.ConfigureServices` method:
2016-10-29 01:35:15 +08:00
Update live with current master (#4749) * Tabbifying script examples per OS (#4735) * Command line deployment * typo fix * edits per review * increased visibility of 'pub to azure' scenarios * removed copy/paste debris * Tabbified the local code segments * fixed merge whoops * attempting to fix TOC * Fixed toc xref * Update implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application.md (#4745) * Update adding-controller1.md (#4747) * Update adding-controller1.md * Update adding-controller1.md * Update publish-to-azure-webapp-using-vs.md (#4746) * Overhaul of resource-based auth doc (#4715) * Updates to resource based auth doc * More edits * More edits * Tag with mvc metadata * Verbiage tweaks * More edits * More MVC edits * More edits * Initial commit of sample app * Replace inline code with code imports * Add download link to sample app * Correct the requirement class name * Adjust line number * Verbiage tweak * More edits * Add 1.x sample * Update sample apps folder name in link * Minor edits * More edits * Fix metadata warnings * Code cleanup * Update description metadata * Update property name in csproj file * Update old namespaces in ASP.NET Core 2.x app * Update .NET Fx target framework TODO comments * Prevent action code from scrolling * Add Razor Pages sample * Fix line numbers for highlighting * Return ForbidResult when appropriate in 2.0 app * Add authentication assumption notes * Update metadata * React to Rick's feedback * Add 1.x handler snippets * Update adding_view1.md
2017-11-09 09:26:35 +08:00
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Startup.cs?name=snippet_ConfigureServicesSample&highlight=3-7,9)]
### Operational requirements
If you're making decisions based on the outcomes of CRUD (**C**reate, **R**ead, **U**pdate, **D**elete) operations, use the [OperationAuthorizationRequirement](/dotnet/api/microsoft.aspnetcore.authorization.infrastructure.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[](resourcebased/samples/ResourceBasedAuthApp2/Services/DocumentAuthorizationCrudHandler.cs?name=snippet_OperationsClass)]
The handler is implemented as follows, using an `OperationAuthorizationRequirement` requirement and a `Document` resource:
# [ASP.NET Core 2.x](#tab/aspnetcore2x)
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Services/DocumentAuthorizationCrudHandler.cs?name=snippet_Handler)]
# [ASP.NET Core 1.x](#tab/aspnetcore1x)
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp1/Services/DocumentAuthorizationCrudHandler.cs?name=snippet_Handler)]
---
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](#tab/aspnetcore2x)
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp2/Pages/Document/View.cshtml.cs?name=snippet_DocumentViewHandler&highlight=10-11)]
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](#tab/aspnetcore1x)
[!code-csharp[](resourcebased/samples/ResourceBasedAuthApp1/Controllers/DocumentController.cs?name=snippet_DocumentViewAction&highlight=11-12)]
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.
---