|
1 | 1 | using Microsoft.AspNetCore.Authentication; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
2 | 3 |
|
3 | 4 | namespace AspNetCore.SecurityKey; |
4 | 5 |
|
| 6 | +/// <summary> |
| 7 | +/// Extension methods to configure security API key authentication. |
| 8 | +/// </summary> |
5 | 9 | public static class AuthenticationBuilderExtensions |
6 | 10 | { |
| 11 | + /// <summary> |
| 12 | + /// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the default scheme. |
| 13 | + /// The default scheme is specified by <see cref="SecurityKeyAuthenticationDefaults.AuthenticationScheme"/>. |
| 14 | + /// </summary> |
| 15 | + /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> |
| 16 | + /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> |
7 | 17 | public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder) |
| 18 | + => builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. |
| 22 | + /// </summary> |
| 23 | + /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> |
| 24 | + /// <param name="authenticationScheme">The authentication scheme.</param> |
| 25 | + /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> |
| 26 | + public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme) |
| 27 | + => builder.AddSecurityKey(authenticationScheme, configureOptions: null!); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the default scheme. |
| 31 | + /// The default scheme is specified by <see cref="SecurityKeyAuthenticationDefaults.AuthenticationScheme"/>. |
| 32 | + /// </summary> |
| 33 | + /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> |
| 34 | + /// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> |
| 35 | + /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> |
| 36 | + public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) |
| 37 | + => builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme, configureOptions); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. |
| 41 | + /// </summary> |
| 42 | + /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> |
| 43 | + /// <param name="authenticationScheme">The authentication scheme.</param> |
| 44 | + /// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> |
| 45 | + /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> |
| 46 | + public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) |
| 47 | + => builder.AddSecurityKey(authenticationScheme, displayName: null, configureOptions: configureOptions); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. |
| 51 | + /// </summary> |
| 52 | + /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> |
| 53 | + /// <param name="authenticationScheme">The authentication scheme.</param> |
| 54 | + /// <param name="displayName">A display name for the authentication handler.</param> |
| 55 | + /// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> |
| 56 | + /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> |
| 57 | + public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) |
8 | 58 | { |
9 | | - return builder.AddScheme<SecurityKeyAuthenticationSchemeOptions, SecurityKeyAuthenticationHandler>( |
10 | | - SecurityKeyAuthenticationDefaults.AuthenticationScheme, |
11 | | - options => { } |
12 | | - ); |
| 59 | + builder.Services.AddOptions<SecurityKeyAuthenticationSchemeOptions>(authenticationScheme); |
| 60 | + return builder.AddScheme<SecurityKeyAuthenticationSchemeOptions, SecurityKeyAuthenticationHandler>(authenticationScheme, displayName, configureOptions); |
13 | 61 | } |
14 | 62 | } |
0 commit comments