using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.DependencyInjection; namespace AuthStudy.Authentication.Browser { public static class BrowserAuthenticationExtensions { public static IServiceCollection AddBrowserAuthentication ( this IServiceCollection builder, string AuthenticationSchemeName, string AuthenticationDispalyName, BrowserAuthenticationOptions Option ) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.AddService(Option.DefaultAuthenticateScheme, Option); builder.AddAuthentication(options => { options.DefaultScheme = AuthenticationSchemeName; options.DefaultAuthenticateScheme = AuthenticationSchemeName; options.DefaultChallengeScheme = AuthenticationSchemeName; options.DefaultForbidScheme = AuthenticationSchemeName; options.DefaultSignInScheme = AuthenticationSchemeName; options.DefaultSignOutScheme = AuthenticationSchemeName; options.AddScheme(AuthenticationSchemeName, AuthenticationDispalyName); }); return builder; } private static IServiceCollection AddService(this IServiceCollection builder, string defaultSchemeName, BrowserAuthenticationOptions option) { defaultSchemeName = defaultSchemeName ?? BrowserAuthenticationDefault.SchemeName; BrowserAuthenticationOptions defaultOption = option ?? new(); defaultOption.DefaultScheme ??= defaultSchemeName; defaultOption.DefaultAuthenticateScheme ??= defaultSchemeName; defaultOption.DefaultChallengeScheme ??= defaultSchemeName; defaultOption.DefaultForbidScheme ??= defaultSchemeName; builder.AddSingleton(defaultOption); builder.AddSingleton(); return builder; } } }