From 244ab48ef7480f433dcbed62552896188d9520dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A3=8A=E7=A3=8A?= Date: Fri, 2 Sep 2022 09:52:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(Identity):=20Fix=20the=20problem=20that=20I?= =?UTF-8?q?UserSetter=20temporarily=20changes=20t=E2=80=A6=20(#226)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(Identity): Fix the problem that IUserSetter temporarily changes the user to obtain user information error * chore: remove invalid parameters --- .../UserContext.cs | 4 ++- .../TestIdentity.cs | 31 +++++++++++++++++++ .../Helpers/RedisHelper.cs | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/UserContext.cs b/src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/UserContext.cs index 50c34daa4..410420795 100644 --- a/src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/UserContext.cs +++ b/src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/UserContext.cs @@ -48,7 +48,9 @@ protected UserContext(ITypeConvertProvider typeConvertProvider) public IDisposable Change(TIdentityUser identityUser) where TIdentityUser : IIdentityUser { - var userModelType = typeof(TIdentityUser); + ArgumentNullException.ThrowIfNull(identityUser); + + var userModelType = identityUser.GetType(); var user = GetUser(userModelType); CurrentUser[userModelType] = identityUser; return new DisposeAction(() => CurrentUser[userModelType] = user); diff --git a/src/Contrib/Authentication/Tests/Masa.Contrib.Authentication.Identity.Tests/TestIdentity.cs b/src/Contrib/Authentication/Tests/Masa.Contrib.Authentication.Identity.Tests/TestIdentity.cs index c210e9312..965a45391 100644 --- a/src/Contrib/Authentication/Tests/Masa.Contrib.Authentication.Identity.Tests/TestIdentity.cs +++ b/src/Contrib/Authentication/Tests/Masa.Contrib.Authentication.Identity.Tests/TestIdentity.cs @@ -234,6 +234,37 @@ public void TestTemporarilyUserReturnUserIdEqual1() Assert.AreEqual(null, isolationUserContext.GetTenantId()); } + [TestMethod] + public void TestChange() + { + var services = new ServiceCollection(); + services.AddMasaIdentityModel(); + + var serviceProvider = services.BuildServiceProvider(); + var userSetter = serviceProvider.GetService(); + Assert.IsNotNull(userSetter); + + IIdentityUser identityUser = new IdentityUser() + { + Id = "1" + }; + using (userSetter.Change(identityUser)) + { + var userContext = serviceProvider.GetService(); + Assert.IsNotNull(userContext); + Assert.AreEqual("1", userContext.UserId); + + Assert.AreEqual(1, userContext.GetUserId()); + } + + IIdentityUser? identityUser2 = null; + + Assert.ThrowsException(() => + { + userSetter.Change(identityUser2!); + }); + } + [TestMethod] public void TestCustomerUserModelReturnTrueNameEqualLisi() { diff --git a/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/Helpers/RedisHelper.cs b/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/Helpers/RedisHelper.cs index d03c716ad..ab7d95991 100644 --- a/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/Helpers/RedisHelper.cs +++ b/src/Utils/Caching/Distributed/Masa.Utils.Caching.Redis/Helpers/RedisHelper.cs @@ -35,7 +35,7 @@ public static class RedisHelper public static RedisValue ConvertFromValue(T value) { - var type = typeof(T); + var type = value?.GetType() ?? typeof(T); dynamic redisValue; switch (GetCompressMode(type)) {