-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
PassThroughAuthorizationHandler.cs
46 lines (40 loc) · 1.62 KB
/
PassThroughAuthorizationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Authorization.Infrastructure;
/// <summary>
/// Infrastructure class which allows an <see cref="IAuthorizationRequirement"/> to
/// be its own <see cref="IAuthorizationHandler"/>.
/// </summary>
public class PassThroughAuthorizationHandler : IAuthorizationHandler
{
private readonly AuthorizationOptions _options;
/// <summary>
/// Creates a new instance of <see cref="PassThroughAuthorizationHandler"/>.
/// </summary>
public PassThroughAuthorizationHandler() : this(Options.Create(new AuthorizationOptions()))
{ }
/// <summary>
/// Creates a new instance of <see cref="PassThroughAuthorizationHandler"/>.
/// </summary>
/// <param name="options">The <see cref="AuthorizationOptions"/> used.</param>
public PassThroughAuthorizationHandler(IOptions<AuthorizationOptions> options)
=> _options = options.Value;
/// <summary>
/// Makes a decision if authorization is allowed.
/// </summary>
/// <param name="context">The authorization context.</param>
public async Task HandleAsync(AuthorizationHandlerContext context)
{
foreach (var handler in context.Requirements.OfType<IAuthorizationHandler>())
{
await handler.HandleAsync(context).ConfigureAwait(false);
if (!_options.InvokeHandlersAfterFailure && context.HasFailed)
{
break;
}
}
}
}