| | |
| | | /*_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; |
| | | public long TenantId => GetUserInfoFromToken(nameof(TenantId)).FirstOrDefault()?.ObjToLong() ?? -1; |
| | | |
| | | // return ""; |
| | | //} |
| | | public int RoleId => GetUserInfoFromToken(ClaimTypes.Role).FirstOrDefault()?.ObjToInt() ?? 0; |
| | | |
| | | public int UserId => GetClaimValueByType("jti") == null ? 0 : GetClaimValueByType("jti").FirstOrDefault().ObjToInt(); |
| | | public long TenantId => UserInfo.TenantId; |
| | | |
| | | public int RoleId => UserInfo.RoleId; |
| | | |
| | | public string Token => "throw new NotImplementedException()"; |
| | | |
| | | //public int SystemType => UserInfo.SystemType; |
| | | public string Token => GetToken(); |
| | | |
| | | 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 ", ""); |
| | | } |
| | | |
| | | private UserInfo _userInfo { get; set; } |
| | | private UserInfo UserInfo |
| | | { |
| | | 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(); |
| | | } |
| | | return _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "") ?? ""; |
| | | } |
| | | |
| | | 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) |
| | | { |
| | |
| | | return new List<string>() { }; |
| | | } |
| | | |
| | | //public MessageModel<string> MessageModel { get; set; } |
| | | |
| | | public IEnumerable<Claim> GetClaimsIdentity() |
| | | { |
| | | if (_accessor.HttpContext != null) |
| | |
| | | |
| | | return claims; |
| | | } |
| | | return null; |
| | | return ArraySegment<Claim>.Empty; |
| | | } |
| | | |
| | | 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; } |
| | | |