using EntrustSettle.Common.Swagger; using EntrustSettle.Model; using EntrustSettle.Model.Models.DJY; using Microsoft.AspNetCore.Http; using SqlSugar; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; namespace EntrustSettle.Common.HttpContextUser { public class DJYUser : IUser { private readonly IHttpContextAccessor _accessor; private User _djyuser; public DJYUser(IHttpContextAccessor accessor) { _accessor = accessor; } public string ID => _djyuser?.GID ?? GetClaimValueByType("loginid").FirstOrDefault() ?? string.Empty; public string Name => _djyuser?.SHOWNAME ?? string.Empty; public string CompanyId => _djyuser?.CompId ?? string.Empty; public string CompanyName => _djyuser?.COMNAME ?? string.Empty; public bool IsSetUser => _djyuser != null; public void SetUser(object user) { _djyuser = user as User; } public bool IsAuthenticated() { return _accessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false; } public string GetToken() { var 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()) { 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; } } return token; } public List GetUserInfoFromToken(string ClaimType) { var jwtHandler = new JwtSecurityTokenHandler(); var token = ""; token = GetToken(); // token校验 if (token.IsNotEmptyOrNull() && jwtHandler.CanReadToken(token)) { JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token); return (from item in jwtToken.Claims where item.Type == ClaimType select item.Value).ToList(); } return new List() { }; } public MessageModel MessageModel { get; set; } public IEnumerable GetClaimsIdentity() { if (_accessor.HttpContext == null) return ArraySegment.Empty; if (!IsAuthenticated()) return GetClaimsIdentity(GetToken()); var claims = _accessor.HttpContext.User.Claims.ToList(); //var headers = _accessor.HttpContext.Request.Headers; //foreach (var header in headers) //{ // claims.Add(new Claim(header.Key, header.Value)); //} return claims; } public IEnumerable 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(); } public List GetClaimValueByType(string ClaimType) { return (from item in GetClaimsIdentity() where item.Type == ClaimType select item.Value).ToList(); } //适配项目 //public long ID => GetClaimValueByType("loginid").FirstOrDefault().ObjToLong(); //public long TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToLong(); } }