|
@@ -22,6 +22,8 @@ public interface ISessionContextManager
|
|
/// 依据source创建对应Session
|
|
/// 依据source创建对应Session
|
|
/// </summary>
|
|
/// </summary>
|
|
void ChangeSessionContext(string source);
|
|
void ChangeSessionContext(string source);
|
|
|
|
+
|
|
|
|
+ void ChangeSessionContext(ISessionContext sessionContext);
|
|
}
|
|
}
|
|
|
|
|
|
public class SessionContextManager : ISessionContextManager, IScopeDependency
|
|
public class SessionContextManager : ISessionContextManager, IScopeDependency
|
|
@@ -49,22 +51,32 @@ public class SessionContextManager : ISessionContextManager, IScopeDependency
|
|
.Includes(d => d.Roles)
|
|
.Includes(d => d.Roles)
|
|
.FirstAsync(d => d.Id == userId, cancellation);
|
|
.FirstAsync(d => d.Id == userId, cancellation);
|
|
|
|
|
|
- List<Claim> userClaims =
|
|
|
|
- [
|
|
|
|
- new(JwtClaimTypes.Subject, account.Id),
|
|
|
|
- new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
|
|
|
|
- new(ClaimTypes.NameIdentifier, account.Id),
|
|
|
|
- new(AppClaimTypes.UserDisplayName, account.Name),
|
|
|
|
- new(AppClaimTypes.DepartmentId, account.User?.OrgId ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.DepartmentIsCenter, account.User?.Organization?.IsCenter.ToString() ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.DepartmentName, account.User?.Organization?.Name ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.DepartmentAreaCode, account.User?.Organization?.AreaCode ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.DepartmentAreaName, account.User?.Organization?.AreaName ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.DepartmentLevel, account.User?.Organization?.Level.ToString() ?? string.Empty),
|
|
|
|
- new(AppClaimTypes.AreaId, account.User?.OrgId?.GetHigherOrgId() ?? string.Empty),
|
|
|
|
- ];
|
|
|
|
- userClaims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
|
|
|
|
- httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
|
|
|
|
|
|
+ ChangeSession(account.Id, account.Name,
|
|
|
|
+ account.User?.OrgId, account.User?.Organization?.Name,
|
|
|
|
+ account.User?.Organization?.Level,
|
|
|
|
+ account.User?.Organization?.IsCenter,
|
|
|
|
+ account.PhoneNo,
|
|
|
|
+ account.User?.Organization?.AreaCode,
|
|
|
|
+ account.User?.Organization?.AreaName,
|
|
|
|
+ account.Roles.Select(d=>d.Name).ToList()
|
|
|
|
+ );
|
|
|
|
+ //
|
|
|
|
+ // List<Claim> userClaims =
|
|
|
|
+ // [
|
|
|
|
+ // new(JwtClaimTypes.Subject, account.Id),
|
|
|
|
+ // new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
|
|
|
|
+ // new(ClaimTypes.NameIdentifier, account.Id),
|
|
|
|
+ // new(AppClaimTypes.UserDisplayName, account.Name),
|
|
|
|
+ // new(AppClaimTypes.DepartmentId, account.User?.OrgId ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.DepartmentIsCenter, account.User?.Organization?.IsCenter.ToString() ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.DepartmentName, account.User?.Organization?.Name ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.DepartmentAreaCode, account.User?.Organization?.AreaCode ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.DepartmentAreaName, account.User?.Organization?.AreaName ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.DepartmentLevel, account.User?.Organization?.Level.ToString() ?? string.Empty),
|
|
|
|
+ // new(AppClaimTypes.AreaId, account.User?.OrgId?.GetHigherOrgId() ?? string.Empty),
|
|
|
|
+ // ];
|
|
|
|
+ // userClaims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
|
|
|
|
+ // httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -86,9 +98,27 @@ public class SessionContextManager : ISessionContextManager, IScopeDependency
|
|
ChangeSession(config.UserId, config.UserName, config.OrgId, config.OrgName, config.OrgId.CalcOrgLevel());
|
|
ChangeSession(config.UserId, config.UserName, config.OrgId, config.OrgName, config.OrgId.CalcOrgLevel());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void ChangeSessionContext(ISessionContext sessionContext)
|
|
|
|
+ {
|
|
|
|
+ ChangeSession(sessionContext.UserId, sessionContext.UserName,
|
|
|
|
+ sessionContext.OrgId, sessionContext.OrgName,
|
|
|
|
+ sessionContext.OrgLevel, sessionContext.OrgIsCenter,
|
|
|
|
+ sessionContext.Phone,
|
|
|
|
+ sessionContext.OrgAreaCode, sessionContext.OrgAreaName,
|
|
|
|
+ sessionContext.Roles.ToList());
|
|
|
|
+ }
|
|
|
|
+
|
|
#region private
|
|
#region private
|
|
|
|
|
|
- private void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
|
|
|
|
|
|
+ private void ChangeSession(
|
|
|
|
+ string userId, string username,
|
|
|
|
+ string orgId, string orgname,
|
|
|
|
+ int? orgLevel,
|
|
|
|
+ bool? isCenter = null,
|
|
|
|
+ string? phoneNo = null,
|
|
|
|
+ string? orgAreaCode = null,
|
|
|
|
+ string? orgAreaName = null,
|
|
|
|
+ List<string>? roles = null)
|
|
{
|
|
{
|
|
var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
|
|
var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
|
|
if (httpContextAccessor.HttpContext == null)
|
|
if (httpContextAccessor.HttpContext == null)
|
|
@@ -99,12 +129,19 @@ public class SessionContextManager : ISessionContextManager, IScopeDependency
|
|
new(JwtClaimTypes.Subject, userId),
|
|
new(JwtClaimTypes.Subject, userId),
|
|
new(ClaimTypes.NameIdentifier, userId),
|
|
new(ClaimTypes.NameIdentifier, userId),
|
|
new(AppClaimTypes.UserDisplayName, username),
|
|
new(AppClaimTypes.UserDisplayName, username),
|
|
|
|
+ new(JwtClaimTypes.PhoneNumber, phoneNo ?? string.Empty),
|
|
new(AppClaimTypes.DepartmentId, orgId ?? string.Empty),
|
|
new(AppClaimTypes.DepartmentId, orgId ?? string.Empty),
|
|
- new(AppClaimTypes.DepartmentIsCenter, orgId?.IsCenter().ToString() ?? string.Empty),
|
|
|
|
new(AppClaimTypes.DepartmentName, orgname ?? string.Empty),
|
|
new(AppClaimTypes.DepartmentName, orgname ?? string.Empty),
|
|
- new(AppClaimTypes.DepartmentLevel, orgLevel.ToString() ?? string.Empty),
|
|
|
|
|
|
+ new(AppClaimTypes.DepartmentIsCenter, isCenter.HasValue
|
|
|
|
+ ? isCenter.Value.ToString()
|
|
|
|
+ : (orgId?.IsCenter().ToString() ?? string.Empty)),
|
|
|
|
+ new(AppClaimTypes.DepartmentAreaCode, orgAreaCode ?? string.Empty),
|
|
|
|
+ new(AppClaimTypes.DepartmentAreaName, orgAreaName ?? string.Empty),
|
|
|
|
+ new(AppClaimTypes.DepartmentLevel, orgLevel?.ToString() ?? string.Empty),
|
|
new(AppClaimTypes.AreaId, orgId?.GetHigherOrgId() ?? string.Empty),
|
|
new(AppClaimTypes.AreaId, orgId?.GetHigherOrgId() ?? string.Empty),
|
|
];
|
|
];
|
|
|
|
+ if (roles != null && roles.Count > 0)
|
|
|
|
+ userClaims.AddRange(roles.Select(d => new Claim(JwtClaimTypes.Role, d)));
|
|
httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
|
|
httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
|
|
}
|
|
}
|
|
|
|
|