Browse Source

Automatically set `RemoteRefreshUrl` in `AddAbpJwtBearer` and `AddAbpOpenIdConnect`.

pull/18175/head
maliming 3 years ago
parent
commit
a2cbd47726
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 36
      framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs
  2. 13
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs

36
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<JwtBearerOptions> configureOptions)
=> builder.AddAbpJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions);
public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action<JwtBearerOptions> configureOptions)
=> builder.AddAbpJwtBearer(authenticationScheme, "Bearer", configureOptions);
public static AuthenticationBuilder AddAbpJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<JwtBearerOptions> configureOptions)
{
builder.Services.Configure<AbpClaimsPrincipalFactoryOptions>(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);
});
}
}

13
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<OpenIdConnectOptions> configureOptions)
{
builder.Services.Configure<AbpClaimsPrincipalFactoryOptions>(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<IOpenIdLocalUserCreationClient>();

Loading…
Cancel
Save