|  |  |  | 
|---|
|  |  |  | using System.Security.Claims; | 
|---|
|  |  |  | using System.Text; | 
|---|
|  |  |  | using System.Threading.Tasks; | 
|---|
|  |  |  | using WIDESEA_Core.Authorization; | 
|---|
|  |  |  | using WIDESEA_Core.Caches; | 
|---|
|  |  |  | using WIDESEA_Core.Const; | 
|---|
|  |  |  | using WIDESEA_Core.DB; | 
|---|
|  |  |  | using WIDESEA_Core.Extensions; | 
|---|
|  |  |  | using WIDESEA_Core.Helper; | 
|---|
|  |  |  | using WIDESEA_Core.Seed; | 
|---|
|  |  |  | using ICacheService = WIDESEA_Core.Caches.ICacheService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEA_Core.HttpContextUser | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public string GetToken() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | string token = _cacheService.Get(UserId.ToString()); | 
|---|
|  |  |  | if (!string.IsNullOrEmpty(token)) { return token; } | 
|---|
|  |  |  | return string.Empty; | 
|---|
|  |  |  | //return _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; | 
|---|
|  |  |  | string token = ""; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (MenuType == 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | token = _cacheService.Get(UserId.ToString()); | 
|---|
|  |  |  | if (!string.IsNullOrEmpty(token)) { return token; } | 
|---|
|  |  |  | token = _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; | 
|---|
|  |  |  | if (!token.IsNullOrEmpty()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | token = _cacheService.Get(UserId.ToString() + "-App"); | 
|---|
|  |  |  | if (!string.IsNullOrEmpty(token)) { return token; } | 
|---|
|  |  |  | token = _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; | 
|---|
|  |  |  | if (!token.IsNullOrEmpty()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void UpdateToke(string token) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _cacheService.AddOrUpdate(UserId.ToString(), token); | 
|---|
|  |  |  | if (MenuType == 0) | 
|---|
|  |  |  | _cacheService.AddOrUpdate(UserId.ToString(), token); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | _cacheService.AddOrUpdate(UserId.ToString() + "-App", token); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void UpdateToke(string token, int userId) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (MenuType == 0) | 
|---|
|  |  |  | _cacheService.AddOrUpdate(userId.ToString(), token); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | _cacheService.AddOrUpdate(userId.ToString() + "-App", token); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public bool IsSuperAdmin => IsRoleIdSuperAdmin(RoleId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public bool IsHighestRole => UserId == 1; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<string> GetUserInfoFromToken(string ClaimType) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | { | 
|---|
|  |  |  | JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return (from item in jwtToken.Claims | 
|---|
|  |  |  | where item.Type == ClaimType | 
|---|
|  |  |  | select item.Value).ToList(); | 
|---|
|  |  |  | List<string> items = (from item in jwtToken.Claims | 
|---|
|  |  |  | where item.Type == ClaimType | 
|---|
|  |  |  | select item.Value).ToList(); | 
|---|
|  |  |  | return items; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return new List<string>() { }; | 
|---|
|  |  |  | 
|---|
|  |  |  | return ArraySegment<Claim>.Empty; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public IEnumerable<Claim> GetClaimsIdentity(string token) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var jwtHandler = new JwtSecurityTokenHandler(); | 
|---|
|  |  |  | // token鏍¢獙 | 
|---|
|  |  |  | if (token.IsNotEmptyOrNull() && jwtHandler.CanReadToken(token)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var jwtToken = jwtHandler.ReadJwtToken(token); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return jwtToken.Claims; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return new List<Claim>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<string> GetClaimValueByType(string ClaimType) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return (from item in GetClaimsIdentity() | 
|---|