-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathClaimExtensions.cs
More file actions
115 lines (104 loc) · 4.82 KB
/
ClaimExtensions.cs
File metadata and controls
115 lines (104 loc) · 4.82 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.ComponentModel;
using System.Security.Claims;
namespace SimpleAuthentication;
/// <summary>
/// Provides extension methods for streamlining working with claims.
/// </summary>
public static class ClaimExtensions
{
/// <summary>
/// Adds or updates the claim with the specified <paramref name="type"/>.
/// </summary>
/// <param name="claims">The claims list</param>
/// <param name="type">The type of the claim to update</param>
/// <param name="value">The value of the claim</param>
/// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="value"/> is <see langword="null"/>.</exception>
/// <seealso cref="Claim"/>
public static void Update(this IList<Claim> claims, string type, string value)
{
claims.Remove(type);
claims.Add(new Claim(type, value));
}
/// <summary>
/// Removes the first occurrence of the claim with the specified <paramref name="type"/>.
/// </summary>
/// <param name="claims">The claims list</param>
/// <param name="type">The type of the claim to remove</param>
/// <returns><see langword="true"/> if item was successfully removed; otherwise, <see langword="false"/>. This method also returns <see langword="false"/> if item is not found.</returns>
/// <seealso cref="Claim"/>
public static bool Remove(this IList<Claim> claims, string type)
{
var claim = claims.FirstOrDefault(c => c.Type == type);
return claims.Remove(claim!);
}
/// <summary>
/// Gets all the values for the claim with the specified <paramref name="type"/>.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="type">The type of the claim to match.</param>
/// <returns>The list of claim values.</returns>
/// <seealso cref="ClaimsPrincipal"/>
public static IEnumerable<string?> GetClaimValues(this ClaimsPrincipal user, string type)
=> user.GetClaimValues<string>(type);
/// <summary>
/// Gets all the values for the claim with the specified <paramref name="type"/>, casted as <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The .NET type of the claim.</typeparam>
/// <param name="user">The user.</param>
/// <param name="type">The type of the claim to match.</param>
/// <returns>The list of claim values.</returns>
/// <seealso cref="ClaimsPrincipal"/>
public static IEnumerable<T?> GetClaimValues<T>(this ClaimsPrincipal user, string type)
{
var value = user.FindAll(type).Select(c => Convert<T>(c.Value)).ToList();
return value;
}
/// <summary>
/// Gets the value of the first claim of the specified <paramref name="type"/>.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="type">The type of the claim to match.</param>
/// <returns>The claim value.</returns>
/// <seealso cref="ClaimsPrincipal"/>
public static string? GetClaimValue(this ClaimsPrincipal user, string type)
=> user.GetClaimValue<string>(type);
/// <summary>
/// Gets the value of the first claim of the specified <paramref name="type"/>, casted as <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="user">The user.</param>
/// <param name="type">The type of the claim to match.</param>
/// <returns>The claim value.</returns>
/// <seealso cref="ClaimsPrincipal"/>
public static T? GetClaimValue<T>(this ClaimsPrincipal user, string type)
{
var value = user.FindFirstValue(type);
if (value is null)
{
return default;
}
return Convert<T>(value);
}
/// <summary>
/// Checks if the user has the specified claim <paramref name="type"/>.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="type">The type of the claim to match.</param>
/// <returns><see langword="true"/> if the user has the requested claim; <see langword="false"/> otherwise.</returns>
/// <seealso cref="ClaimsPrincipal"/>
public static bool HasClaim(this ClaimsPrincipal user, string type)
{
var hasClaim = user.Claims.Any(c => c.Type == type);
return hasClaim;
}
/// <summary>
/// Adds a collection of string values in the list of claims with the same claim type
/// </summary>
/// <param name="claims">The claims list</param>
/// <param name="type">The type of the claims that will be added</param>
/// <param name="values">The collection of values</param>
public static IList<Claim> Union(this IList<Claim> claims, string type, IEnumerable<string> values)
=> claims.Union(values.Select(value => new Claim(type, value))).ToList();
private static T? Convert<T>(string value)
=> (T?)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(value);
}