ClaimsAccessor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Ropin.Inspection.Common.Accessor.Interface;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Ropin.Inspection.Common.Accessor
  9. {
  10. public class ClaimsAccessor : IClaimsAccessor
  11. {
  12. protected IPrincipalAccessor PrincipalAccessor { get; }
  13. public ClaimsAccessor(IPrincipalAccessor principalAccessor)
  14. {
  15. PrincipalAccessor = principalAccessor;
  16. }
  17. /// <summary>
  18. /// 登录用户ID
  19. /// </summary>
  20. public Guid ApiUserId
  21. {
  22. get
  23. {
  24. var userId = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
  25. if (userId != null)
  26. {
  27. return Guid.Parse(userId);
  28. }else
  29. {
  30. throw new Exception("登录用户ID不存在");
  31. }
  32. //return Guid.Empty;
  33. }
  34. }
  35. /// <summary>
  36. /// 登录用户名称
  37. /// </summary>
  38. public string ApiUserName
  39. {
  40. get
  41. {
  42. var userName = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
  43. if (string.IsNullOrWhiteSpace(userName))
  44. {
  45. return string.Empty;
  46. }
  47. return userName;
  48. }
  49. }
  50. /// <summary>
  51. /// 用户角色Id
  52. /// </summary>
  53. public string RoleIds
  54. {
  55. get
  56. {
  57. var roleIds = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;
  58. if (string.IsNullOrWhiteSpace(roleIds))
  59. {
  60. return string.Empty;
  61. }
  62. return roleIds;
  63. }
  64. }
  65. /// <summary>
  66. /// 组织架构ID
  67. /// </summary>
  68. public Guid OrganizeId
  69. {
  70. get
  71. {
  72. var primaryGroupSid = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.PrimaryGroupSid)?.Value;
  73. if (!string.IsNullOrEmpty(primaryGroupSid) )
  74. {
  75. return Guid.Parse(primaryGroupSid);
  76. }
  77. else
  78. {
  79. throw new Exception("组织架构不存在");
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// LinsenceCode
  85. /// </summary>
  86. public string Linsence
  87. {
  88. get
  89. {
  90. var primaryLinsence = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Rsa)?.Value;
  91. if (string.IsNullOrEmpty(primaryLinsence))
  92. {
  93. //throw new Exception("LinsenceCode不存在");
  94. return null;
  95. }
  96. else
  97. {
  98. return primaryLinsence;
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// LicenseTypeCode
  104. /// </summary>
  105. public string LicenseTypeCode
  106. {
  107. get
  108. {
  109. var licenseTypeCode = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid)?.Value;
  110. if (string.IsNullOrEmpty(licenseTypeCode))
  111. {
  112. //throw new Exception("LicenseTypeCode不存在");
  113. return null;
  114. }
  115. else
  116. {
  117. return licenseTypeCode;
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// 用户组织架构类型
  123. /// </summary>
  124. public string OrgTypeCode
  125. {
  126. get
  127. {
  128. var orgTypeCode = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Actor)?.Value;
  129. if (string.IsNullOrEmpty(orgTypeCode))
  130. {
  131. throw new Exception("组织架构类型不存在");
  132. }
  133. else
  134. {
  135. return orgTypeCode;
  136. }
  137. }
  138. }
  139. }
  140. }