| | |
| | | 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.Helper; |
| | | using WIDESEA_Core.Seed; |
| | | using ICacheService = WIDESEA_Core.Caches.ICacheService; |
| | | |
| | | namespace WIDESEA_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").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 = _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), |
| | | ConnectionString = AppSettings.app(MainDb.ConnectionString), |
| | | 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, |
| | | SystemType = userInfo.SystemType, |
| | | 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.SystemType |
| | | }, |
| | | new() { |
| | | FieldName = MainDb.UserName |
| | | } |
| | | }; |
| | | return selectModels; |
| | | } |
| | | |
| | | public List<string> GetUserInfoFromToken(string ClaimType) |
| | | { |
| | |
| | | return new List<string>() { }; |
| | | } |
| | | |
| | | //public MessageModel<string> MessageModel { get; set; } |
| | | |
| | | public IEnumerable<Claim> GetClaimsIdentity() |
| | | { |
| | | var claims = _accessor.HttpContext.User.Claims.ToList(); |
| | | var headers = _accessor.HttpContext.Request.Headers; |
| | | foreach (var header in headers) |
| | | if (_accessor.HttpContext != null) |
| | | { |
| | | claims.Add(new Claim(header.Key, header.Value)); |
| | | } |
| | | 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; |
| | | return claims; |
| | | } |
| | | return ArraySegment<Claim>.Empty; |
| | | } |
| | | |
| | | public List<string> GetClaimValueByType(string ClaimType) |
| | |
| | | |
| | | public int RoleId { get; set; } |
| | | |
| | | public int SystemType { get; set; } |
| | | |
| | | public string UserName { get; set; } |
| | | |
| | | public int UserId { get; set; } |
| | | public int UserId { get; set; } |
| | | |
| | | public string UserTrueName { get; set; } |
| | | |