| | |
| | | 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; |
| | | |
| | | namespace WIDESEAWCS_Core.HttpContextUser |
| | | { |
| | | public class AspNetUser : IUser |
| | | { |
| | | private readonly IHttpContextAccessor _accessor; |
| | | private readonly ICacheService _cacheService; |
| | | //private readonly ILogger<AspNetUser> _logger; |
| | | |
| | | public AspNetUser(IHttpContextAccessor accessor/*, ILogger<AspNetUser> logger*/) |
| | | public AspNetUser(IHttpContextAccessor accessor, ICacheService cacheService) |
| | | { |
| | | _accessor = accessor; |
| | | _cacheService = cacheService; |
| | | /*_logger = logger;*/ |
| | | } |
| | | |
| | | public string UserName => UserInfo.UserName; |
| | | public string UserName => GetUserInfoFromToken(ClaimTypes.Name).FirstOrDefault() ?? ""; |
| | | |
| | | //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(); |
| | | // } |
| | | // } |
| | | public int UserId => GetClaimValueByType(JwtRegisteredClaimNames.Jti) == null ? 0 : GetClaimValueByType(JwtRegisteredClaimNames.Jti).FirstOrDefault()?.ObjToInt() ?? 0; |
| | | |
| | | // return ""; |
| | | //} |
| | | public long TenantId => GetUserInfoFromToken(nameof(TenantId)).FirstOrDefault()?.ObjToLong() ?? -1; |
| | | |
| | | public int UserId => GetClaimValueByType("jti") == null ? 0 : GetClaimValueByType("jti").FirstOrDefault().ObjToInt(); |
| | | public long TenantId => UserInfo.TenantId; |
| | | public int RoleId => GetUserInfoFromToken(ClaimTypes.Role).FirstOrDefault()?.ObjToInt() ?? 0; |
| | | |
| | | public int RoleId => UserInfo.RoleId; |
| | | public string Token => GetToken(); |
| | | |
| | | public string Token => "throw new NotImplementedException()"; |
| | | |
| | | //public int SystemType => UserInfo.SystemType; |
| | | public int MenuType => (_accessor.HttpContext?.Request.Headers.ContainsKey("uniapp") ?? false) ? 1 : 0; |
| | | |
| | | public bool IsAuthenticated() |
| | | { |
| | | return _accessor.HttpContext.User.Identity.IsAuthenticated; |
| | | return _accessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false; |
| | | } |
| | | |
| | | |
| | | public string GetToken() |
| | | { |
| | | return _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", ""); |
| | | 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 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 ", "") ?? ""; |
| | | } |
| | | |
| | | private UserInfo _userInfo { get; set; } |
| | | private UserInfo UserInfo |
| | | public void UpdateToke(string token) |
| | | { |
| | | get |
| | | { |
| | | if (_userInfo != null) |
| | | return _userInfo; |
| | | |
| | | SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig |
| | | { |
| | | ConfigId = MainDb.CurrentDbConnId, |
| | | DbType = MainDb.DbType, |
| | | ConnectionString = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB), |
| | | IsAutoCloseConnection = true |
| | | }); |
| | | |
| | | dynamic userInfo = sqlSugarClient.Queryable(MainDb.UserTableName, "x").Where(MainDb.UserId, "=", UserId).Select(GetUserInfoSelectModels()).First(); |
| | | if (userInfo != null) |
| | | { |
| | | _userInfo = new UserInfo() |
| | | { |
| | | RoleId = userInfo.Role_Id, |
| | | TenantId = userInfo.TenantId, |
| | | UserName = userInfo.UserName, |
| | | }; |
| | | return _userInfo; |
| | | } |
| | | |
| | | return new UserInfo(); |
| | | } |
| | | _cacheService.AddOrUpdate(UserId.ToString(), token); |
| | | } |
| | | |
| | | public bool IsSuperAdmin => IsRoleIdSuperAdmin(RoleId); |
| | | |
| | | public List<Permissions> Permissions => throw new NotImplementedException(); |
| | | |
| | | public UserInfo GetCurrentUserInfo() |
| | | { |
| | | return UserInfo; |
| | | } |
| | | |
| | | public List<SelectModel> GetUserInfoSelectModels() |
| | | { |
| | | List<SelectModel> selectModels = new List<SelectModel>() |
| | | { |
| | | new() { |
| | | FieldName = MainDb.TenantId |
| | | }, |
| | | new() { |
| | | FieldName = MainDb.RoleId, |
| | | }, |
| | | new() { |
| | | FieldName = MainDb.UserName |
| | | } |
| | | }; |
| | | return selectModels; |
| | | } |
| | | |
| | | 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>() { }; |
| | | } |
| | | |
| | | //public MessageModel<string> MessageModel { get; set; } |
| | | |
| | | public IEnumerable<Claim> GetClaimsIdentity() |
| | | { |
| | |
| | | |
| | | return claims; |
| | | } |
| | | return null; |
| | | 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) |
| | | { |
| | | IEnumerable<Claim> claims = GetClaimsIdentity(); |
| | | if (claims != null) |
| | | return (from item in GetClaimsIdentity() |
| | | where item.Type == ClaimType |
| | | select item.Value).ToList(); |
| | | return null; |
| | | return (from item in GetClaimsIdentity() |
| | | where item.Type == ClaimType |
| | | select item.Value).ToList(); |
| | | } |
| | | |
| | | public bool IsRoleIdSuperAdmin(int roleId) |
| | |
| | | public long TenantId { get; set; } |
| | | |
| | | public int RoleId { get; set; } |
| | | |
| | | public int SystemType { get; set; } |
| | | |
| | | public string UserName { get; set; } |
| | | |