| | | 1 | | using Microsoft.AspNetCore.Authentication; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Authentication.Providers.OpenIdConnect; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for registering OpenID Connect authentication provider services. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class OpenIdConnectAuthenticationServiceExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Adds the OpenID Connect authentication provider registration. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="builder">The authentication builder used to register authentication handlers.</param> |
| | | 15 | | /// <param name="options">The OpenID Connect provider options.</param> |
| | | 16 | | /// <returns>The same <see cref="AuthenticationBuilder"/> instance for chaining.</returns> |
| | | 17 | | public static AuthenticationBuilder AddOpenIdConnectAuthentication( |
| | | 18 | | this AuthenticationBuilder builder, |
| | | 19 | | OpenIdConnectAuthenticationOptions options) |
| | | 20 | | { |
| | 182 | 21 | | ArgumentNullException.ThrowIfNull(builder); |
| | 180 | 22 | | ArgumentNullException.ThrowIfNull(options); |
| | | 23 | | |
| | 178 | 24 | | if (!options.Enabled) |
| | | 25 | | { |
| | 172 | 26 | | return builder; |
| | | 27 | | } |
| | | 28 | | |
| | 6 | 29 | | builder.AddOpenIdConnect(options.Scheme, options.DisplayName, openIdConnectOptions => |
| | 6 | 30 | | { |
| | 2 | 31 | | openIdConnectOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 2 | 32 | | openIdConnectOptions.Authority = options.Authority; |
| | 2 | 33 | | openIdConnectOptions.ClientId = options.ClientId; |
| | 2 | 34 | | openIdConnectOptions.ClientSecret = options.ClientSecret; |
| | 2 | 35 | | openIdConnectOptions.CallbackPath = options.CallbackPath; |
| | 2 | 36 | | openIdConnectOptions.ResponseType = options.ResponseType; |
| | 2 | 37 | | openIdConnectOptions.SaveTokens = options.SaveTokens; |
| | 6 | 38 | | |
| | 2 | 39 | | openIdConnectOptions.Scope.Clear(); |
| | 6 | 40 | | |
| | 24 | 41 | | foreach (string scope in options.Scopes.Where(scope => !string.IsNullOrWhiteSpace(scope))) |
| | 6 | 42 | | { |
| | 6 | 43 | | openIdConnectOptions.Scope.Add(scope); |
| | 6 | 44 | | } |
| | 8 | 45 | | }); |
| | | 46 | | |
| | 6 | 47 | | return builder; |
| | | 48 | | } |
| | | 49 | | } |