diff --git a/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs new file mode 100644 index 0000000000..cd35e5d377 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Volo.Abp.Security.Claims; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class AbpOpenIdConnectExtensions +{ + public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder) + => builder.AddAbpJwtBearer(JwtBearerDefaults.AuthenticationScheme, _ => { }); + + public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder, Action configureOptions) + => builder.AddAbpJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions); + + public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + => builder.AddAbpJwtBearer(authenticationScheme, "Bearer", configureOptions); + + public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + builder.Services.Configure(options => + { + var jwtBearerOption = new JwtBearerOptions(); + configureOptions?.Invoke(jwtBearerOption); + if (!jwtBearerOption.Authority.IsNullOrEmpty()) + { + options.RemoteRefreshUrl = jwtBearerOption.Authority.RemovePostFix("/") + options.RemoteRefreshUrl; + } + }); + + return builder.AddJwtBearer(authenticationScheme, displayName, options => + { + configureOptions?.Invoke(options); + }); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs index 7d2c1fe6d6..d07cca857f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Volo.Abp.AspNetCore.Authentication.OpenIdConnect; using Volo.Abp.AspNetCore.MultiTenancy; +using Volo.Abp.Security.Claims; namespace Microsoft.Extensions.DependencyInjection; @@ -24,6 +25,16 @@ public static class AbpOpenIdConnectExtensions public static AuthenticationBuilder AddAbpOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) { + builder.Services.Configure(options => + { + var openIdConnectOptions = new OpenIdConnectOptions(); + configureOptions?.Invoke(openIdConnectOptions); + if (!openIdConnectOptions.Authority.IsNullOrEmpty()) + { + options.RemoteRefreshUrl = openIdConnectOptions.Authority.RemovePostFix("/") + options.RemoteRefreshUrl; + } + }); + return builder.AddOpenIdConnect(authenticationScheme, displayName, options => { options.ClaimActions.MapAbpClaimTypes(); @@ -38,7 +49,7 @@ public static class AbpOpenIdConnectExtensions }; options.AccessDeniedPath = "/"; - + options.Events.OnTokenValidated = async (context) => { var client = context.HttpContext.RequestServices.GetRequiredService();