| | |
| | | using System.Security.Claims; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core.Authorization; |
| | | using WIDESEAWCS_Core.Const; |
| | | using WIDESEAWCS_Core.DB; |
| | | using WIDESEAWCS_Core.Extensions; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_Core.Seed; |
| | | using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; |
| | |
| | | |
| | | public string GetToken() |
| | | { |
| | | string token = _cacheService.Get(UserId.ToString()); |
| | | string token = _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; |
| | | if (!token.IsNullOrEmpty()) |
| | | { |
| | | return token; |
| | | } |
| | | if (_accessor.HttpContext?.IsSuccessSwagger() == true) |
| | | { |
| | | token = _accessor.HttpContext.GetSuccessSwaggerJwt(); |
| | | if (token.IsNotEmptyOrNull()) |
| | | { |
| | | //UserInfo userInfo = JwtHelper.SerializeJwt(token); |
| | | //if (userInfo.UserId > 0) |
| | | //{ |
| | | // return token; |
| | | //} |
| | | List<Claim> claims1 = _accessor.HttpContext.User.Claims.ToList(); |
| | | if (_accessor.HttpContext.User.Claims.Any(s => s.Type == JwtRegisteredClaimNames.Jti)) |
| | | { |
| | | return token; |
| | | } |
| | | |
| | | var claims = new ClaimsIdentity(GetClaimsIdentity(token)); |
| | | _accessor.HttpContext.User.AddIdentity(claims); |
| | | return token; |
| | | } |
| | | } |
| | | token = _cacheService.Get(UserId.ToString()); |
| | | if (!string.IsNullOrEmpty(token)) { return token; } |
| | | return string.Empty; |
| | | return token; |
| | | |
| | | // string token = _cacheService.Get(UserId.ToString()); |
| | | //if (!string.IsNullOrEmpty(token)) { return token; } |
| | | //return string.Empty; |
| | | //return _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; |
| | | } |
| | | |
| | |
| | | { |
| | | 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() |