-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
AuthorizationResult.cs
49 lines (40 loc) · 1.83 KB
/
AuthorizationResult.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
namespace Microsoft.AspNetCore.Authorization;
/// <summary>
/// Encapsulates the result of <see cref="IAuthorizationService.AuthorizeAsync(ClaimsPrincipal, object, IEnumerable{IAuthorizationRequirement})"/>.
/// </summary>
public class AuthorizationResult
{
private static readonly AuthorizationResult _succeededResult = new() { Succeeded = true };
private static readonly AuthorizationResult _failedResult = new() { Failure = AuthorizationFailure.ExplicitFail() };
private AuthorizationResult() { }
/// <summary>
/// True if authorization was successful.
/// </summary>
[MemberNotNullWhen(false, nameof(Failure))]
public bool Succeeded { get; private set; }
/// <summary>
/// Contains information about why authorization failed.
/// </summary>
public AuthorizationFailure? Failure { get; private set; }
/// <summary>
/// Returns a successful result.
/// </summary>
/// <returns>A successful result.</returns>
public static AuthorizationResult Success() => _succeededResult;
/// <summary>
/// Creates a failed authorization result.
/// </summary>
/// <param name="failure">Contains information about why authorization failed.</param>
/// <returns>The <see cref="AuthorizationResult"/>.</returns>
public static AuthorizationResult Failed(AuthorizationFailure failure) => new AuthorizationResult { Failure = failure };
/// <summary>
/// Creates a failed authorization result.
/// </summary>
/// <returns>The <see cref="AuthorizationResult"/>.</returns>
public static AuthorizationResult Failed() => _failedResult;
}