UserManager.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 当前登录用户
  4. /// </summary>
  5. public class UserManager : IScoped
  6. {
  7. private readonly IHttpContextAccessor _httpContextAccessor;
  8. private long _tenantId;
  9. public long UserId
  10. {
  11. get => long.Parse(_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value);
  12. }
  13. public long TenantId
  14. {
  15. get
  16. {
  17. var tId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.TenantId)?.Value;
  18. return string.IsNullOrWhiteSpace(tId) ? _tenantId : long.Parse(tId);
  19. }
  20. set => _tenantId = value;
  21. }
  22. public string Account
  23. {
  24. get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Account)?.Value;
  25. }
  26. public string RealName
  27. {
  28. get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.RealName)?.Value;
  29. }
  30. public bool SuperAdmin
  31. {
  32. get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString();
  33. }
  34. public long OrgId
  35. {
  36. get
  37. {
  38. var orgId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OrgId)?.Value;
  39. return string.IsNullOrWhiteSpace(orgId) ? 0 : long.Parse(orgId);
  40. }
  41. }
  42. public string OpenId
  43. {
  44. get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OpenId)?.Value;
  45. }
  46. public UserManager(IHttpContextAccessor httpContextAccessor)
  47. {
  48. _httpContextAccessor = httpContextAccessor;
  49. }
  50. }