-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBasicAuthenticationSettings.cs
More file actions
69 lines (60 loc) · 3.23 KB
/
BasicAuthenticationSettings.cs
File metadata and controls
69 lines (60 loc) · 3.23 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
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
namespace SimpleAuthentication.BasicAuthentication;
/// <summary>
/// Options class provides information needed to control Basic Authentication handler behavior.
/// </summary>
/// <seealso cref="AuthenticationSchemeOptions"/>
public class BasicAuthenticationSettings : AuthenticationSchemeOptions
{
/// <summary>
/// Gets or sets the authentication scheme name (Default: Basic).
/// </summary>
public string SchemeName { get; set; } = BasicAuthenticationDefaults.AuthenticationScheme;
/// <summary>
/// Gets or sets a fixed value to compare the user name against.
/// If you need to perform custom checks to validate the authentication, you should leave this value to <see langword="null"/>, as well as <see cref="Password"/>, and register an <see cref="IBasicAuthenticationValidator"/> service.
/// </summary>
/// <seealso cref="Password"/>
/// <seealso cref="IBasicAuthenticationValidator"/>
public string? UserName { get; set; }
/// <summary>
/// Gets or sets a fixed value to compare the password against.
/// If you need to perform custom checks to validate the authentication, you should leave this value to <see langword="null"/>, as well as <see cref="UserName"/>, and register an <see cref="IBasicAuthenticationValidator"/> service.
/// </summary>
/// <seealso cref="UserName"/>
/// <seealso cref="IBasicAuthenticationValidator"/>
public string? Password { get; set; }
/// <summary>
/// The collection of authorization credentials.
/// </summary>
/// <seealso cref="Credential"/>
public IEnumerable<Credential> Credentials { get; set; } = [];
/// <summary>
/// Gets or sets a <see cref="string"/> that defines the <see cref="ClaimsIdentity.NameClaimType"/>.
/// </summary>
/// <remarks>
/// Controls the value <see cref="ClaimsIdentity.Name"/> returns. It will return the first <see cref="Claim.Value"/> where the <see cref="Claim.Type"/> equals <see cref="NameClaimType"/>.
/// The default is <see cref="ClaimsIdentity.DefaultNameClaimType"/>.
/// </remarks>
public string NameClaimType { get; set; } = ClaimsIdentity.DefaultNameClaimType;
/// <summary>
/// Gets or sets the <see cref="string"/> that defines the <see cref="ClaimsIdentity.RoleClaimType"/>.
/// </summary>
/// <remarks>
/// <para>Controls the results of <see cref="ClaimsPrincipal.IsInRole( string )"/>.</para>
/// <para>Each <see cref="Claim"/> where <see cref="Claim.Type"/> == <see cref="RoleClaimType"/> will be checked for a match against the 'string' passed to <see cref="ClaimsPrincipal.IsInRole(string)"/>.</para>
/// The default is <see cref="ClaimsIdentity.DefaultRoleClaimType"/>.
/// </remarks>
public string RoleClaimType { get; set; } = ClaimsIdentity.DefaultRoleClaimType;
internal IEnumerable<Credential> GetAllCredentials()
{
var credentials = (Credentials ?? []).ToHashSet();
if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password))
{
// If necessary, add the Credentials from the base properties.
credentials.Add(new(UserName, Password));
}
return credentials;
}
}