Browse Source

Add `WebAssemblyAuthenticationStateProvider`.

pull/19479/head
maliming 3 years ago
parent
commit
029e7ac25b
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 3
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpBlazorWebAppServiceCollectionExtensions.cs
  2. 19
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs
  3. 56
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyAuthenticationStateProvider.cs
  4. 12
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs
  5. 23
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentPrincipalAccessor.cs
  6. 8
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteCurrentPrincipalAccessor.cs

3
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpBlazorWebAppServiceCollectionExtensions.cs

@ -4,7 +4,6 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp; using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp;
using Volo.Abp.Http.Client.Authentication; using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Security.Claims;
namespace Microsoft.Extensions.DependencyInjection; namespace Microsoft.Extensions.DependencyInjection;
@ -16,7 +15,6 @@ public static class AbpBlazorWebAppServiceCollectionExtensions
services.AddSingleton<AuthenticationStateProvider, RemoteAuthenticationStateProvider>(); services.AddSingleton<AuthenticationStateProvider, RemoteAuthenticationStateProvider>();
services.Replace(ServiceDescriptor.Transient<IAbpAccessTokenProvider, CookieBasedWebAssemblyAbpAccessTokenProvider>()); services.Replace(ServiceDescriptor.Transient<IAbpAccessTokenProvider, CookieBasedWebAssemblyAbpAccessTokenProvider>());
services.Replace(ServiceDescriptor.Transient<ICurrentPrincipalAccessor, RemoteCurrentPrincipalAccessor>());
return services; return services;
} }
@ -27,7 +25,6 @@ public static class AbpBlazorWebAppServiceCollectionExtensions
services.AddScoped<AuthenticationStateProvider, RemoteAuthenticationStateProvider>(); services.AddScoped<AuthenticationStateProvider, RemoteAuthenticationStateProvider>();
services.Replace(ServiceDescriptor.Singleton<IAbpAccessTokenProvider, PersistentComponentStateAbpAccessTokenProvider>()); services.Replace(ServiceDescriptor.Singleton<IAbpAccessTokenProvider, PersistentComponentStateAbpAccessTokenProvider>());
services.Replace(ServiceDescriptor.Transient<ICurrentPrincipalAccessor, RemoteCurrentPrincipalAccessor>());
return services; return services;
} }

19
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs

@ -1,6 +1,8 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp.AspNetCore.Components.Web; using Volo.Abp.AspNetCore.Components.Web;
@ -47,6 +49,23 @@ public class AbpAspNetCoreComponentsWebAssemblyModule : AbpModule
.AddProvider(new AbpExceptionHandlingLoggerProvider(context.Services)); .AddProvider(new AbpExceptionHandlingLoggerProvider(context.Services));
} }
public override void PostConfigureServices(ServiceConfigurationContext context)
{
var msAuthenticationStateProvider = context.Services.FirstOrDefault(x => x.ServiceType == typeof(AuthenticationStateProvider));
if (msAuthenticationStateProvider != null && msAuthenticationStateProvider.ImplementationType != null)
{
var webAssemblyAuthenticationStateProviderType = typeof(WebAssemblyAuthenticationStateProvider<,,>).MakeGenericType(
msAuthenticationStateProvider.ImplementationType.GenericTypeArguments[0],
msAuthenticationStateProvider.ImplementationType.GenericTypeArguments[1],
msAuthenticationStateProvider.ImplementationType.GenericTypeArguments[2]);
context.Services.AddScoped(webAssemblyAuthenticationStateProviderType, webAssemblyAuthenticationStateProviderType);
context.Services.Remove(msAuthenticationStateProvider);
context.Services.AddScoped(typeof(AuthenticationStateProvider), sp => sp.GetRequiredService(webAssemblyAuthenticationStateProviderType));
}
}
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override void OnApplicationInitialization(ApplicationInitializationContext context)
{ {
AsyncHelper.RunSync(() => OnApplicationInitializationAsync(context)); AsyncHelper.RunSync(() => OnApplicationInitializationAsync(context));

56
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyAuthenticationStateProvider.cs

@ -0,0 +1,56 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class WebAssemblyAuthenticationStateProvider<TRemoteAuthenticationState, TAccount, TProviderOptions> : RemoteAuthenticationService<TRemoteAuthenticationState, TAccount, TProviderOptions>
where TRemoteAuthenticationState : RemoteAuthenticationState
where TProviderOptions : new()
where TAccount : RemoteUserAccount
{
protected WebAssemblyCachedApplicationConfigurationClient WebAssemblyCachedApplicationConfigurationClient { get; }
[Obsolete]
public WebAssemblyAuthenticationStateProvider(
IJSRuntime jsRuntime,
IOptionsSnapshot<RemoteAuthenticationOptions<TProviderOptions>> options,
NavigationManager navigation,
AccountClaimsPrincipalFactory<TAccount> accountClaimsPrincipalFactory,
ILogger<WebAssemblyAuthenticationStateProvider<TRemoteAuthenticationState, TAccount, TProviderOptions>> logger,
WebAssemblyCachedApplicationConfigurationClient webAssemblyCachedApplicationConfigurationClient)
: base(jsRuntime, options, navigation, accountClaimsPrincipalFactory)
{
WebAssemblyCachedApplicationConfigurationClient = webAssemblyCachedApplicationConfigurationClient;
}
public WebAssemblyAuthenticationStateProvider(
IJSRuntime jsRuntime,
IOptionsSnapshot<RemoteAuthenticationOptions<TProviderOptions>> options,
NavigationManager navigation,
AccountClaimsPrincipalFactory<TAccount> accountClaimsPrincipalFactory,
ILogger<RemoteAuthenticationService<TRemoteAuthenticationState, TAccount, TProviderOptions>>? logger,
ILogger<WebAssemblyAuthenticationStateProvider<TRemoteAuthenticationState, TAccount, TProviderOptions>> logger1,
WebAssemblyCachedApplicationConfigurationClient webAssemblyCachedApplicationConfigurationClient)
: base(jsRuntime, options, navigation, accountClaimsPrincipalFactory, logger)
{
WebAssemblyCachedApplicationConfigurationClient = webAssemblyCachedApplicationConfigurationClient;
}
public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{
var state = await base.GetAuthenticationStateAsync();
var applicationConfigurationDto = await WebAssemblyCachedApplicationConfigurationClient.GetAsync();
if (state.User.Identity != null && state.User.Identity.IsAuthenticated && !applicationConfigurationDto.CurrentUser.IsAuthenticated)
{
await WebAssemblyCachedApplicationConfigurationClient.InitializeAsync();
}
return state;
}
}

12
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.JSInterop;
using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.AspNetCore.Components.Web.Security;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ClientProxies; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ClientProxies;
@ -20,18 +21,22 @@ public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicatio
protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; } protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; }
protected IJSRuntime JSRuntime { get; }
public WebAssemblyCachedApplicationConfigurationClient( public WebAssemblyCachedApplicationConfigurationClient(
AbpApplicationConfigurationClientProxy applicationConfigurationClientProxy, AbpApplicationConfigurationClientProxy applicationConfigurationClientProxy,
ApplicationConfigurationCache cache, ApplicationConfigurationCache cache,
ICurrentTenantAccessor currentTenantAccessor, ICurrentTenantAccessor currentTenantAccessor,
AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy, AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy,
ApplicationConfigurationChangedService applicationConfigurationChangedService) ApplicationConfigurationChangedService applicationConfigurationChangedService,
IJSRuntime jsRuntime)
{ {
ApplicationConfigurationClientProxy = applicationConfigurationClientProxy; ApplicationConfigurationClientProxy = applicationConfigurationClientProxy;
Cache = cache; Cache = cache;
CurrentTenantAccessor = currentTenantAccessor; CurrentTenantAccessor = currentTenantAccessor;
ApplicationLocalizationClientProxy = applicationLocalizationClientProxy; ApplicationLocalizationClientProxy = applicationLocalizationClientProxy;
ApplicationConfigurationChangedService = applicationConfigurationChangedService; ApplicationConfigurationChangedService = applicationConfigurationChangedService;
JSRuntime = jsRuntime;
} }
public virtual async Task InitializeAsync() public virtual async Task InitializeAsync()
@ -53,6 +58,11 @@ public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicatio
Cache.Set(configurationDto); Cache.Set(configurationDto);
if (!configurationDto.CurrentUser.IsAuthenticated)
{
await JSRuntime.InvokeVoidAsync("sessionStorage.clear");
}
ApplicationConfigurationChangedService.NotifyChanged(); ApplicationConfigurationChangedService.NotifyChanged();
CurrentTenantAccessor.Current = new BasicTenantInfo( CurrentTenantAccessor.Current = new BasicTenantInfo(

23
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentPrincipalAccessor.cs

@ -1,23 +0,0 @@
using System.Security.Claims;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Components.Web.Security;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class WebAssemblyCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITransientDependency
{
protected AbpComponentsClaimsCache ClaimsCache { get; }
public WebAssemblyCurrentPrincipalAccessor(
IClientScopeServiceProviderAccessor clientScopeServiceProviderAccessor)
{
ClaimsCache = clientScopeServiceProviderAccessor.ServiceProvider.GetRequiredService<AbpComponentsClaimsCache>();
}
protected override ClaimsPrincipal GetClaimsPrincipal()
{
return ClaimsCache.Principal;
}
}

8
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebApp/RemoteCurrentPrincipalAccessor.cs → framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteCurrentPrincipalAccessor.cs

@ -3,13 +3,13 @@ using System.Security.Claims;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims; using Volo.Abp.Security.Claims;
namespace Volo.Abp.AspNetCore.Components.WebAssembly.WebApp; namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class RemoteCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITransientDependency public class WebAssemblyRemoteCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITransientDependency
{ {
protected ApplicationConfigurationCache ApplicationConfigurationCache { get; } protected ApplicationConfigurationCache ApplicationConfigurationCache { get; }
public RemoteCurrentPrincipalAccessor(ApplicationConfigurationCache applicationConfigurationCache) public WebAssemblyRemoteCurrentPrincipalAccessor(ApplicationConfigurationCache applicationConfigurationCache)
{ {
ApplicationConfigurationCache = applicationConfigurationCache; ApplicationConfigurationCache = applicationConfigurationCache;
} }
@ -84,6 +84,6 @@ public class RemoteCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITra
} }
} }
return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: nameof(RemoteCurrentPrincipalAccessor))); return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: nameof(WebAssemblyRemoteCurrentPrincipalAccessor)));
} }
} }
Loading…
Cancel
Save