using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; namespace Admin.Core.Common { public class AspNetUser : IUser { private readonly IHttpContextAccessor _accessor; private readonly ILogger _logger; public AspNetUser(IHttpContextAccessor accessor, ILogger logger) { _accessor = accessor; _logger = logger; } public string Name => GetName(); private string GetName() { if (IsAuthenticated() && _accessor.HttpContext.User.Identity.Name.IsNotEmptyOrNull()) { return _accessor.HttpContext.User.Identity.Name; } else { if (!string.IsNullOrEmpty(GetToken())) { var getNameType = Permissions.IsUseIds4 ? "name" : "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"; return GetUserInfoFromToken(getNameType).FirstOrDefault().ObjToString(); } } return ""; } public int ID => GetClaimValueByType("jti").FirstOrDefault().ObjToInt(); public bool IsAuthenticated() { return _accessor.HttpContext.User.Identity.IsAuthenticated; } public string GetToken() { return _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", ""); } 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 IEnumerable GetClaimsIdentity() { return _accessor.HttpContext.User.Claims; } public List GetClaimValueByType(string ClaimType) { return (from item in GetClaimsIdentity() where item.Type == ClaimType select item.Value).ToList(); } } }