diff --git a/src/Infrastructure/Extensions/AuthenticationSetup.cs b/src/Infrastructure/Extensions/AuthenticationSetup.cs index ccfa540..e3af9ae 100644 --- a/src/Infrastructure/Extensions/AuthenticationSetup.cs +++ b/src/Infrastructure/Extensions/AuthenticationSetup.cs @@ -14,11 +14,15 @@ public static class AuthenticationSetup /// /// /// 自定义认证服务配置 + /// 配置认证选项 + /// 配置jwt认证选项 /// public static IServiceCollection AddDefaultAuthentication( this IServiceCollection services, IConfiguration configuration, - Action? builderOptions = null) + Action? builderOptions = null, + Action? configureAuthenticationOptions = null, + Action? configureJwtBearerOptions = null) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configuration); @@ -55,11 +59,16 @@ public static class AuthenticationSetup options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = nameof(DefaultAuthenticationHandler); options.DefaultForbidScheme = nameof(DefaultAuthenticationHandler); + configureAuthenticationOptions?.Invoke(options); }); builder.AddScheme( nameof(DefaultAuthenticationHandler), options => { }); - builder.AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters; }); + builder.AddJwtBearer(options => + { + options.TokenValidationParameters = tokenValidationParameters; + configureJwtBearerOptions?.Invoke(options); + }); builderOptions?.Invoke(builder); return services; } diff --git a/src/Infrastructure/Extensions/AuthorizeSetup.cs b/src/Infrastructure/Extensions/AuthorizeSetup.cs index cf5dd69..de93ab5 100644 --- a/src/Infrastructure/Extensions/AuthorizeSetup.cs +++ b/src/Infrastructure/Extensions/AuthorizeSetup.cs @@ -1,4 +1,5 @@ using Infrastructure.Options; +using Microsoft.AspNetCore.Authorization; using Microsoft.IdentityModel.Tokens; namespace Infrastructure.Extensions; @@ -10,10 +11,12 @@ public static class AuthorizeSetup /// /// /// + /// /// public static IServiceCollection AddDefaultAuthorize( this IServiceCollection services, - IConfiguration configuration) + IConfiguration configuration, + Action? configureAuthorizeBuilder = null) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configuration); @@ -35,10 +38,11 @@ public static class AuthorizeSetup audienceOptions.Duration, signingCredentials)); - services.AddAuthorizationBuilder() - .AddPolicy(audienceOptions.Policy!, policy => - policy.RequireRole(audienceOptions.Roles!) - .Build()); + var builder = services.AddAuthorizationBuilder(); + builder.AddPolicy(audienceOptions.Policy!, policy => + policy.RequireRole(audienceOptions.Roles!) + .Build()); + configureAuthorizeBuilder?.Invoke(builder); return services; } } \ No newline at end of file diff --git a/src/Infrastructure/Extensions/ControllerSetup.cs b/src/Infrastructure/Extensions/ControllerSetup.cs index 95e99f0..1755d03 100644 --- a/src/Infrastructure/Extensions/ControllerSetup.cs +++ b/src/Infrastructure/Extensions/ControllerSetup.cs @@ -13,16 +13,23 @@ public static class ControllerSetup /// 配置controller,包含过滤器、json序列化以及模型验证等配置 /// /// - /// + /// 配置Controllers + /// mvc配置选项 + /// json序列化配置选项 + /// api行为配置选项 /// public static IServiceCollection AddDefaultControllers(this IServiceCollection services, - Action? configureControllers = null) + Action? configureControllers = null, + Action? configureMvcOptions = null, + Action? configureMvcNewtonsoftJsonOptions = null, + Action? configureApiBehaviorOptions = null) { ArgumentNullException.ThrowIfNull(services); var mvcBuilder = services.AddControllers(options => { options.Filters.Add(); options.Filters.Add(); + configureMvcOptions?.Invoke(options); }); mvcBuilder.AddNewtonsoftJson(options => @@ -33,6 +40,7 @@ public static class ControllerSetup options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; options.SerializerSettings.Converters.Add(new StringEnumConverter()); + configureMvcNewtonsoftJsonOptions?.Invoke(options); }); mvcBuilder.ConfigureApiBehaviorOptions(options => { @@ -41,6 +49,7 @@ public static class ControllerSetup var message = new MessageData(false, "the input value not valid", 400); return new OkObjectResult(message.Serialize()); }; + configureApiBehaviorOptions?.Invoke(options); }); configureControllers?.Invoke(mvcBuilder); return services;