Skip to content

Commit fe256ee

Browse files
author
Rick Anderson
authored
authorize overview (dotnet#11933)
* authorize overview * authorize overview * authorize overview * authorize overview * authorize overview * authorize overview * authorize overview
1 parent a88922d commit fe256ee

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

aspnetcore/security/authorization/policies.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,87 @@ An authorization policy consists of one or more requirements. It's registered as
1717

1818
In the preceding example, an "AtLeast21" policy is created. It has a single requirement—that of a minimum age, which is supplied as a parameter to the requirement.
1919

20+
## IAuthorizationService
21+
22+
The primary service that determines if authorization is successful is <xref:Microsoft.AspNetCore.Authorization.IAuthorizationService>:
23+
24+
[!code-csharp[](policies/samples/stubs/copy_of_IAuthorizationService.cs?highlight=24-25,48-49&name=snippet)]
25+
26+
The preceding code highlights the two methods of the [IAuthorizationService](https://github.com/aspnet/AspNetCore/blob/v2.2.4/src/Security/Authorization/Core/src/IAuthorizationService.cs).
27+
28+
<xref:Microsoft.AspNetCore.Authorization.IAuthorizationRequirement> is a marker service with no methods, and the mechanism for tracking whether authorization is successful.
29+
30+
Each <xref:Microsoft.AspNetCore.Authorization.IAuthorizationHandler> is responsible for checking if requirements are met:
31+
<!--The following code is a copy/paste from
32+
https://github.com/aspnet/AspNetCore/blob/v2.2.4/src/Security/Authorization/Core/src/IAuthorizationHandler.cs -->
33+
34+
```csharp
35+
/// <summary>
36+
/// Classes implementing this interface are able to make a decision if authorization
37+
/// is allowed.
38+
/// </summary>
39+
public interface IAuthorizationHandler
40+
{
41+
/// <summary>
42+
/// Makes a decision if authorization is allowed.
43+
/// </summary>
44+
/// <param name="context">The authorization information.</param>
45+
Task HandleAsync(AuthorizationHandlerContext context);
46+
}
47+
```
48+
49+
The <xref:Microsoft.AspNetCore.Authorization.AuthorizationHandlerContext> class is what the handler uses to mark whether requirements have been met:
50+
51+
```csharp
52+
context.Succeed(requirement)
53+
```
54+
55+
The following code shows the simplified (and annotated with comments) default implementation of the authorization service:
56+
57+
```csharp
58+
public async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user,
59+
object resource, IEnumerable<IAuthorizationRequirement> requirements)
60+
{
61+
// Create a tracking context from the authorization inputs.
62+
var authContext = _contextFactory.CreateContext(requirements, user, resource);
63+
64+
// By default this returns an IEnumerable<IAuthorizationHandlers> from DI.
65+
var handlers = await _handlers.GetHandlersAsync(authContext);
66+
67+
// Invoke all handlers.
68+
foreach (var handler in handlers)
69+
{
70+
await handler.HandleAsync(authContext);
71+
}
72+
73+
// Check the context, by default success is when all requirements have been met.
74+
return _evaluator.Evaluate(authContext);
75+
}
76+
```
77+
78+
The following code shows a typical `ConfigureServices`:
79+
80+
```csharp
81+
public void ConfigureServices(IServiceCollection services)
82+
{
83+
// Add all of your handlers to DI.
84+
services.AddSingleton<IAuthorizationHandler, MyHandler1>();
85+
// MyHandler2, ...
86+
87+
services.AddSingleton<IAuthorizationHandler, MyHandlerN>();
88+
89+
// Configure your policies
90+
services.AddAuthorization(options =>
91+
options.AddPolicy("Something",
92+
policy => policy.RequireClaim("Permission", "CanViewPage", "CanViewAnything")));
93+
94+
95+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
96+
}
97+
```
98+
99+
Use <xref:Microsoft.AspNetCore.Authorization.IAuthorizationService> or `[Authorize(Policy = "Something"]` for authorization.
100+
20101
## Applying policies to MVC controllers
21102

22103
If you're using Razor Pages, see [Applying policies to Razor Pages](#applying-policies-to-razor-pages) in this document.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// THIS IS A COPY OF https://github.com/aspnet/AspNetCore/blob/v2.2.4/src/Security/Authorization/Core/src/IAuthorizationService.cs
2+
// USED FOR DOCUMENTAION
3+
// Copyright (c) .NET Foundation. All rights reserved.
4+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
5+
6+
using System.Collections.Generic;
7+
using System.Security.Claims;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.AspNetCore.Authorization
11+
{
12+
#region snippet
13+
/// <summary>
14+
/// Checks policy based permissions for a user
15+
/// </summary>
16+
public interface IAuthorizationService
17+
{
18+
/// <summary>
19+
/// Checks if a user meets a specific set of requirements for the specified resource
20+
/// </summary>
21+
/// <param name="user">The user to evaluate the requirements against.</param>
22+
/// <param name="resource">
23+
/// An optional resource the policy should be checked with.
24+
/// If a resource is not required for policy evaluation you may pass null as the value
25+
/// </param>
26+
/// <param name="requirements">The requirements to evaluate.</param>
27+
/// <returns>
28+
/// A flag indicating whether authorization has succeeded.
29+
/// This value is <value>true</value> when the user fulfills the policy;
30+
/// otherwise <value>false</value>.
31+
/// </returns>
32+
/// <remarks>
33+
/// Resource is an optional parameter and may be null. Please ensure that you check
34+
/// it is not null before acting upon it.
35+
/// </remarks>
36+
Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource,
37+
IEnumerable<IAuthorizationRequirement> requirements);
38+
39+
/// <summary>
40+
/// Checks if a user meets a specific authorization policy
41+
/// </summary>
42+
/// <param name="user">The user to check the policy against.</param>
43+
/// <param name="resource">
44+
/// An optional resource the policy should be checked with.
45+
/// If a resource is not required for policy evaluation you may pass null as the value
46+
/// </param>
47+
/// <param name="policyName">The name of the policy to check against a specific
48+
/// context.</param>
49+
/// <returns>
50+
/// A flag indicating whether authorization has succeeded.
51+
/// Returns a flag indicating whether the user, and optional resource has fulfilled
52+
/// the policy.
53+
/// <value>true</value> when the policy has been fulfilled;
54+
/// otherwise <value>false</value>.
55+
/// </returns>
56+
/// <remarks>
57+
/// Resource is an optional parameter and may be null. Please ensure that you check
58+
/// it is not null before acting upon it.
59+
/// </remarks>
60+
Task<AuthorizationResult> AuthorizeAsync(
61+
ClaimsPrincipal user, object resource, string policyName);
62+
}
63+
#endregion
64+
}

0 commit comments

Comments
 (0)