diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/BlazorCurrentPrincipalAccessor.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/BlazorCurrentPrincipalAccessor.cs index d0471959f..c0b049c99 100644 --- a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/BlazorCurrentPrincipalAccessor.cs +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/BlazorCurrentPrincipalAccessor.cs @@ -5,15 +5,15 @@ namespace Masa.Contrib.Authentication.Identity.BlazorWebAssembly; public class BlazorCurrentPrincipalAccessor : ICurrentPrincipalAccessor { - readonly AuthenticationStateProvider _authenticationStateProvider; + protected MasaComponentsClaimsCache ClaimsCache { get; } - public BlazorCurrentPrincipalAccessor(AuthenticationStateProvider authenticationStateProvider) + public BlazorCurrentPrincipalAccessor(IClientScopeServiceProviderAccessor clientScopeServiceProviderAccessor) { - _authenticationStateProvider = authenticationStateProvider; + ClaimsCache = clientScopeServiceProviderAccessor.ServiceProvider.GetRequiredService(); } public ClaimsPrincipal? GetCurrentPrincipal() { - return _authenticationStateProvider.GetAuthenticationStateAsync().Result.User; + return ClaimsCache.Principal; } } diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ComponentsClientScopeServiceProviderAccessor.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ComponentsClientScopeServiceProviderAccessor.cs new file mode 100644 index 000000000..942a90b8c --- /dev/null +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ComponentsClientScopeServiceProviderAccessor.cs @@ -0,0 +1,9 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.Authentication.Identity.BlazorWebAssembly; + +public class ComponentsClientScopeServiceProviderAccessor : IClientScopeServiceProviderAccessor +{ + public IServiceProvider ServiceProvider { get; set; } +} diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/MasaComponentsClaimsCache.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/MasaComponentsClaimsCache.cs new file mode 100644 index 000000000..61bc52ad2 --- /dev/null +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/MasaComponentsClaimsCache.cs @@ -0,0 +1,32 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.Authentication.Identity.BlazorWebAssembly; + +public class MasaComponentsClaimsCache : IScopedDependency +{ + public ClaimsPrincipal Principal { get; private set; } + private readonly AuthenticationStateProvider _authenticationStateProvider; + + public MasaComponentsClaimsCache( + IClientScopeServiceProviderAccessor serviceProviderAccessor) + { + _authenticationStateProvider = serviceProviderAccessor.ServiceProvider.GetService(); + if (_authenticationStateProvider != null) + { + _authenticationStateProvider.AuthenticationStateChanged += async (task) => + { + Principal = (await task).User; + }; + } + } + + public virtual async Task InitializeAsync() + { + if (_authenticationStateProvider != null) + { + var authenticationState = await _authenticationStateProvider.GetAuthenticationStateAsync(); + Principal = authenticationState.User; + } + } +} diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs index f4d85cca4..eff118481 100644 --- a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs @@ -1,7 +1,8 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. // ReSharper disable once CheckNamespace + namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions @@ -31,7 +32,16 @@ public static IServiceCollection AddMasaIdentity( private static IServiceCollection AddMasaIdentityCore(IServiceCollection services) { services.AddAuthorizationCore(); + services.TryAddSingleton(); services.TryAddScoped(); return services; } + + public static async Task InitializeApplicationAsync( + [NotNull] this IServiceProvider serviceProvider) + { + ((ComponentsClientScopeServiceProviderAccessor)serviceProvider + .GetRequiredService()).ServiceProvider = serviceProvider; + await serviceProvider.GetRequiredService().ServiceProvider.GetRequiredService().InitializeAsync(); + } } diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/_Imports.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/_Imports.cs index edb6bae89..644c5fc35 100644 --- a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/_Imports.cs +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/_Imports.cs @@ -3,7 +3,10 @@ global using Masa.Contrib.Authentication.Identity; global using Masa.Contrib.Authentication.Identity.BlazorWebAssembly; +global using Masa.Contrib.Authentication.Identity.Core; global using Microsoft.AspNetCore.Components.Authorization; +global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.DependencyInjection.Extensions; +global using System.Diagnostics.CodeAnalysis; global using System.Security.Claims; global using System.Text.Json; diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/IClientScopeServiceProviderAccessor.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/IClientScopeServiceProviderAccessor.cs new file mode 100644 index 000000000..4979aa422 --- /dev/null +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/IClientScopeServiceProviderAccessor.cs @@ -0,0 +1,9 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.Authentication.Identity.Core; + +public interface IClientScopeServiceProviderAccessor +{ + IServiceProvider ServiceProvider { get; } +} diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/ICurrentPrincipalAccessor.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/ICurrentPrincipalAccessor.cs index c3f86f502..15102f54a 100644 --- a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/ICurrentPrincipalAccessor.cs +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/ICurrentPrincipalAccessor.cs @@ -1,4 +1,4 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. namespace Masa.Contrib.Authentication.Identity; diff --git a/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs b/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs index 6e74974dc..853678ae6 100644 --- a/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs +++ b/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. namespace Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests;