-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
PolicyAuthorizationResult.cs
64 lines (53 loc) · 2.59 KB
/
PolicyAuthorizationResult.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Authorization.Policy;
/// <summary>
/// The result of <see cref="IPolicyEvaluator.AuthorizeAsync(AuthorizationPolicy, Authentication.AuthenticateResult, Http.HttpContext, object?)"/>.
/// </summary>
public class PolicyAuthorizationResult
{
private static readonly PolicyAuthorizationResult _challengedResult = new() { Challenged = true };
private static readonly PolicyAuthorizationResult _forbiddenResult = new() { Forbidden = true };
private static readonly PolicyAuthorizationResult _succeededResult = new() { Succeeded = true };
private PolicyAuthorizationResult() { }
/// <summary>
/// If true, means the callee should challenge and try again.
/// </summary>
public bool Challenged { get; private set; }
/// <summary>
/// Authorization was forbidden.
/// </summary>
public bool Forbidden { get; private set; }
/// <summary>
/// Authorization was successful.
/// </summary>
public bool Succeeded { get; private set; }
/// <summary>
/// Contains information about why authorization failed.
/// </summary>
public AuthorizationFailure? AuthorizationFailure { get; private set; }
/// <summary>
///Indicates that an unauthenticated user requested access to an endpoint that requires authentication.
/// </summary>
/// <returns>The <see cref="PolicyAuthorizationResult"/>.</returns>
public static PolicyAuthorizationResult Challenge() => _challengedResult;
/// <summary>
/// Indicates that the access to a resource was forbidden.
/// </summary>
/// <returns>The <see cref="PolicyAuthorizationResult"/>.</returns>
public static PolicyAuthorizationResult Forbid() => _forbiddenResult;
/// <summary>
/// Indicates that the access to a resource was forbidden.
/// </summary>
/// <param name="authorizationFailure">Specifies the reason the authorization failed.s</param>
/// <returns>The <see cref="PolicyAuthorizationResult"/>.</returns>
public static PolicyAuthorizationResult Forbid(AuthorizationFailure? authorizationFailure)
=> authorizationFailure is null
? _forbiddenResult
: new PolicyAuthorizationResult { Forbidden = true, AuthorizationFailure = authorizationFailure };
/// <summary>
/// Indicates a successful authorization.
/// </summary>
/// <returns>The <see cref="PolicyAuthorizationResult"/>.</returns>
public static PolicyAuthorizationResult Success() => _succeededResult;
}