From 0a6dc69a8b1f58a86b250f4b3fe3f6711f8289b0 Mon Sep 17 00:00:00 2001 From: wzh425 Date: Wed, 28 Aug 2024 10:24:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20BlazorCurrentPr?= =?UTF-8?q?incipalAccessor=20in=20BlazorWebAssembly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlazorCurrentPrincipalAccessor.cs | 8 ++--- ...nentsClientScopeServiceProviderAccessor.cs | 9 ++++++ .../MasaComponentsClaimsCache.cs | 32 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 12 ++++++- .../_Imports.cs | 3 ++ .../IClientScopeServiceProviderAccessor.cs | 9 ++++++ .../ICurrentPrincipalAccessor.cs | 2 +- .../IdentityTest.cs | 2 +- 8 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ComponentsClientScopeServiceProviderAccessor.cs create mode 100644 src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/MasaComponentsClaimsCache.cs create mode 100644 src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/IClientScopeServiceProviderAccessor.cs 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; From 4252c6492c332f4777d77e2c24847cd29c6089e1 Mon Sep 17 00:00:00 2001 From: wzh425 Date: Wed, 28 Aug 2024 11:55:05 +0800 Subject: [PATCH 2/2] test: Adjust unit testing --- .../ServiceCollectionExtensions.cs | 1 + .../IdentityTest.cs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) 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 eff118481..a3cb07e86 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 @@ -32,6 +32,7 @@ public static IServiceCollection AddMasaIdentity( private static IServiceCollection AddMasaIdentityCore(IServiceCollection services) { services.AddAuthorizationCore(); + services.TryAddScoped(); services.TryAddSingleton(); services.TryAddScoped(); return services; 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 853678ae6..3cfc4f53b 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 @@ -21,7 +21,7 @@ public void TestMasaIdentity() } [TestMethod] - public void TestMasaIdentity2() + public async Task TestMasaIdentity2Async() { var services = new ServiceCollection(); var claimsPrincipal = new ClaimsPrincipal(new List() @@ -44,6 +44,9 @@ public void TestMasaIdentity2() option.UserId = "sub"; }); + var serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); + Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); @@ -51,7 +54,7 @@ public void TestMasaIdentity2() Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); - var serviceProvider = services.BuildServiceProvider(); + var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext); Assert.AreEqual("1", userContext.UserId); @@ -63,7 +66,7 @@ public void TestMasaIdentity2() } [TestMethod] - public void TestIdentityByYaml() + public async Task TestIdentityByYamlAsync() { var services = new ServiceCollection(); services.AddMasaIdentity(string.Empty); @@ -95,6 +98,7 @@ public void TestIdentityByYaml() services.AddScoped(_ => authenticationStateProvider.Object); serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext); @@ -108,7 +112,7 @@ public void TestIdentityByYaml() } [TestMethod] - public void TestIdentityByYamlAndCustomOptions() + public async Task TestIdentityByYamlAndCustomOptionsAsync() { var services = new ServiceCollection(); services.AddMasaIdentity(string.Empty, option => @@ -143,6 +147,7 @@ public void TestIdentityByYamlAndCustomOptions() services.AddScoped(_ => authenticationStateProvider.Object); serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext);