xf 2 жил өмнө
parent
commit
0c030206d6

+ 4 - 11
src/Hotline.Api/Context/DefaultSessionContext.cs

@@ -27,17 +27,10 @@ namespace Hotline.Api.Token
             //Roles = user.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray();
             Roles = user.Claims.Where(d => d.Type == ClaimTypes.Role).Select(d => d.Value).ToArray();
 
-            var dbUser = uow.Db.Queryable<User>()
-                .Includes(d => d.Organization)
-                .FirstAsync(d => d.Id == RequiredUserId)
-                .GetAwaiter().GetResult();
-            if (dbUser != null)
-            {
-                UserName ??= dbUser.Name;
-                OrgId = dbUser.OrgId;
-                OrgCode = dbUser.OrgCode;
-                OrgName = dbUser.Organization.OrgName;
-            }
+            OrgId = user.FindFirstValue(AppClaimTypes.DepartmentId);
+            OrgCode = user.FindFirstValue(AppClaimTypes.DepartmentCode);
+            OrgName = user.FindFirstValue(AppClaimTypes.DepartmentName);
+
         }
 
         /// <summary>

+ 1 - 1
src/Hotline.Application/Hotline.Application.csproj

@@ -9,7 +9,7 @@
   <ItemGroup>
     <PackageReference Include="Dapr.AspNetCore" Version="1.9.0" />
     <PackageReference Include="IdentityModel" Version="6.0.0" />
-    <PackageReference Include="XF.Utility.AppIdentityModel" Version="1.0.2" />
+    <PackageReference Include="XF.Utility.AppIdentityModel" Version="1.0.3" />
   </ItemGroup>
 
   <ItemGroup>

+ 13 - 0
src/Hotline.Application/Identity/IdentityAppService.cs

@@ -2,6 +2,7 @@
 using Hotline.Identity.Accounts;
 using Hotline.Share.Dtos.Identity;
 using Hotline.Share.Enums.Identity;
+using Hotline.Users;
 using IdentityModel;
 using Microsoft.AspNetCore.Identity;
 using Microsoft.Extensions.Options;
@@ -17,17 +18,20 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
 {
     private readonly IAccountRepository _accountRepository;
     private readonly IAccountDomainService _accountDomainService;
+    private readonly IUserRepository _userRepository;
     private readonly IJwtSecurity _jwtSecurity;
     private readonly IOptionsSnapshot<IdentityConfiguration> _identityOptionsAccessor;
 
     public IdentityAppService(
         IAccountRepository accountRepository,
         IAccountDomainService accountDomainService,
+        IUserRepository userRepository,
         IJwtSecurity jwtSecurity,
         IOptionsSnapshot<IdentityConfiguration> identityOptionsAccessor)
     {
         _accountRepository = accountRepository;
         _accountDomainService = accountDomainService;
+        _userRepository = userRepository;
         _jwtSecurity = jwtSecurity;
         _identityOptionsAccessor = identityOptionsAccessor;
     }
@@ -61,6 +65,12 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
         account.AccessFailedCount = 0;
         await _accountRepository.UpdateAsync(account, cancellationToken);
 
+        var user = await _userRepository.Queryable()
+            .Includes(d => d.Organization)
+            .FirstAsync(d => d.Id == account.Id);
+        if (user == null)
+            throw UserFriendlyException.SameMessage("未查询到用户数据");
+
         var jwtOptions = _identityOptionsAccessor.Value.Jwt;
         var claims = new List<Claim>
         {
@@ -70,6 +80,9 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
             new(AppClaimTypes.UserDisplayName, account.Name),
             new(JwtClaimTypes.Scope,jwtOptions.Scope),
             new(AppClaimTypes.UserPasswordChanged, account.PasswordChanged.ToString()),
+            new(AppClaimTypes.DepartmentId, user.OrgId??string.Empty),
+            new(AppClaimTypes.DepartmentCode, user.OrgCode??string.Empty),
+            new(AppClaimTypes.DepartmentName, user.Organization?.OrgName??string.Empty),
         };
         claims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
         var token = _jwtSecurity.EncodeJwtToken(claims);