using NCC.Authorization; using NCC.DataEncryption; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.IdentityModel.Tokens; using System; using System.Linq; using System.Reflection; namespace Microsoft.Extensions.DependencyInjection { /// /// JWT 授权服务拓展类 /// public static class JWTAuthorizationServiceCollectionExtensions { /// /// 添加 JWT 授权 /// /// /// token 验证参数 /// /// 启动全局授权 /// public static AuthenticationBuilder AddJwt(this AuthenticationBuilder authenticationBuilder, object tokenValidationParameters = default, Action jwtBearerConfigure = null, bool enableGlobalAuthorize = false) { // 获取框架上下文 _ = JWTEncryption.GetFrameworkContext(Assembly.GetCallingAssembly()); // 配置 JWT 选项 ConfigureJWTOptions(authenticationBuilder.Services); // 添加授权 authenticationBuilder.AddJwtBearer(options => { // 反射获取全局配置 var jwtSettings = JWTEncryption.FrameworkApp.GetMethod("GetOptions").MakeGenericMethod(typeof(JWTSettingsOptions)).Invoke(null, new object[] { null }) as JWTSettingsOptions; // 配置 JWT 验证信息 options.TokenValidationParameters = (tokenValidationParameters as TokenValidationParameters) ?? JWTEncryption.CreateTokenValidationParameters(jwtSettings); // 添加自定义配置 jwtBearerConfigure?.Invoke(options); }); //启用全局授权 if (enableGlobalAuthorize) { authenticationBuilder.Services.Configure(options => { options.Filters.Add(new AuthorizeFilter()); }); } return authenticationBuilder; } /// /// 添加 JWT 授权 /// /// /// 授权配置 /// token 验证参数 /// /// public static AuthenticationBuilder AddJwt(this IServiceCollection services, Action authenticationConfigure = null, object tokenValidationParameters = default, Action jwtBearerConfigure = null) { // 获取框架上下文 _ = JWTEncryption.GetFrameworkContext(Assembly.GetCallingAssembly()); // 添加默认授权 var authenticationBuilder = services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; // 添加自定义配置 authenticationConfigure?.Invoke(options); }); AddJwt(authenticationBuilder, tokenValidationParameters, jwtBearerConfigure); return authenticationBuilder; } /// /// 添加 JWT 授权 /// /// /// /// /// /// /// /// public static AuthenticationBuilder AddJwt(this IServiceCollection services, Action authenticationConfigure = null, object tokenValidationParameters = default, Action jwtBearerConfigure = null, bool enableGlobalAuthorize = false) where TAuthorizationHandler : class, IAuthorizationHandler { // 植入 NCC 框架 var NCCAssembly = JWTEncryption.GetFrameworkContext(Assembly.GetCallingAssembly()); // 获取添加授权类型 var authorizationServiceCollectionExtensionsType = NCCAssembly.GetType("Microsoft.Extensions.DependencyInjection.AuthorizationServiceCollectionExtensions"); var addAppAuthorizationMethod = authorizationServiceCollectionExtensionsType .GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(u => u.Name == "AddAppAuthorization" && u.IsGenericMethod && u.GetParameters().Length > 0 && u.GetParameters()[0].ParameterType == typeof(IServiceCollection)).First(); // 添加策略授权服务 addAppAuthorizationMethod.MakeGenericMethod(typeof(TAuthorizationHandler)).Invoke(null, new object[] { services, null, enableGlobalAuthorize }); // 添加授权 return services.AddJwt(authenticationConfigure, tokenValidationParameters, jwtBearerConfigure); } /// /// 添加 JWT 授权 /// /// private static void ConfigureJWTOptions(IServiceCollection services) { // 配置验证 services.AddOptions() .BindConfiguration("JWTSettings") .ValidateDataAnnotations() .PostConfigure(options => { _ = JWTEncryption.SetDefaultJwtSettings(options); }); } } }