IwbSession.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using Abp.Configuration.Startup;
  6. using Abp.MultiTenancy;
  7. using Abp.Runtime;
  8. using Abp.Runtime.Session;
  9. namespace IwbZero.Runtime.Session
  10. {
  11. public class IwbSession : ClaimsAbpSession, IIwbSession
  12. {
  13. public IwbSession(
  14. IPrincipalAccessor principalAccessor,
  15. IMultiTenancyConfig multiTenancy,
  16. ITenantResolver tenantResolver,
  17. IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) : base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider)
  18. {
  19. }
  20. public string UserName
  21. {
  22. get
  23. {
  24. Claim claim = GetClaim(IwbClaimTypes.UserName);
  25. return claim?.Value ?? "";
  26. }
  27. }
  28. public string PhoneNumber
  29. {
  30. get
  31. {
  32. Claim claim = GetClaim(IwbClaimTypes.PhoneNumber);
  33. return claim?.Value ?? "";
  34. }
  35. }
  36. public string RealName
  37. {
  38. get
  39. {
  40. Claim claim = GetClaim(IwbClaimTypes.RealName);
  41. return claim?.Value ?? "";
  42. }
  43. }
  44. public string EmailAddress
  45. {
  46. get
  47. {
  48. Claim claim = GetClaim(IwbClaimTypes.EmailAddress);
  49. return claim?.Value ?? "";
  50. }
  51. }
  52. public string AvatarImagePath
  53. {
  54. get
  55. {
  56. Claim claim = GetClaim(IwbClaimTypes.AvatarImagePath);
  57. return claim?.Value ?? "";
  58. }
  59. }
  60. public string AccountNo
  61. {
  62. get
  63. {
  64. Claim claim = GetClaim(IwbClaimTypes.AccountNo);
  65. return claim?.Value ?? "";
  66. }
  67. }
  68. public int? UserType
  69. {
  70. get
  71. {
  72. Claim claim = GetClaim(IwbClaimTypes.UserType);
  73. if (string.IsNullOrEmpty(claim?.Value) || !int.TryParse(claim.Value, out var result))
  74. return new int?();
  75. return result;
  76. }
  77. }
  78. public int? AccountType
  79. {
  80. get
  81. {
  82. Claim claim = GetClaim(IwbClaimTypes.AccountType);
  83. if (string.IsNullOrEmpty(claim?.Value) || !int.TryParse(claim.Value, out var result))
  84. return new int?();
  85. return result;
  86. }
  87. }
  88. public List<string> UserRoles
  89. {
  90. get
  91. {
  92. Claim claim = GetClaim(IwbClaimTypes.UserRoles);
  93. if (string.IsNullOrEmpty(claim?.Value))
  94. return new List<string>();
  95. var result = claim.Value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  96. return result;
  97. }
  98. }
  99. public List<string> UserRoleIds
  100. {
  101. get
  102. {
  103. Claim claim = GetClaim(IwbClaimTypes.UserRoleIds);
  104. if (string.IsNullOrEmpty(claim?.Value))
  105. return new List<string>();
  106. var result = claim.Value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  107. return result;
  108. }
  109. }
  110. public Claim GetClaim(string type)
  111. {
  112. ClaimsPrincipal principal = PrincipalAccessor.Principal;
  113. Claim claim = principal?.Claims.FirstOrDefault(c => c.Type == type);
  114. return claim;
  115. }
  116. }
  117. }