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 BaseBrowserAuthenticationExtensions { #region 基于接口的扩展注册 public static IServiceCollection AddBaseBrowserAuthentication ( this IServiceCollection builder, string authenticationSchemeName, string authenticationDisplayName, BrowserAuthenticationOptions authenticationOption ) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.AddService(authenticationOption); builder.AddAuthentication(options => { options.DefaultScheme = authenticationSchemeName; options.DefaultAuthenticateScheme = authenticationSchemeName; options.DefaultChallengeScheme = authenticationSchemeName; options.DefaultForbidScheme = authenticationSchemeName; options.DefaultSignInScheme = authenticationSchemeName; options.DefaultSignOutScheme = authenticationSchemeName; options.AddScheme(authenticationSchemeName, authenticationDisplayName); }); return builder; } private static IServiceCollection AddService(this IServiceCollection builder, BrowserAuthenticationOptions option) { BrowserAuthenticationOptions defaultOption = option ?? new(){AllowBrowsers = BrowserAuthenticationDefault.AllowBrowsers}; builder.AddSingleton(defaultOption); builder.AddSingleton(); return builder; } #endregion } }