UserManager.cs 1.6 KB

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