DefaultSessionContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Security.Authentication;
  2. using System.Security.Claims;
  3. using IdentityModel;
  4. using Microsoft.AspNetCore.Http;
  5. using XF.Domain.Authentications;
  6. using XF.Domain.Dependency;
  7. namespace Hotline.Tests.Controller;
  8. public class DefaultSessionContext : ISessionContext, IScopeDependency
  9. {
  10. private readonly IHttpContextAccessor _contextAccessor;
  11. public DefaultSessionContext(IHttpContextAccessor httpContextAccessor)
  12. {
  13. _contextAccessor = httpContextAccessor;
  14. //Roles = user.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray();
  15. }
  16. private HttpContext _content = new DefaultHttpContext();
  17. private HttpContext GetContext()
  18. {
  19. return _contextAccessor.HttpContext;
  20. }
  21. public HttpContext? HttpContext { get => GetContext(); set => _content = value; }
  22. public string? OpenId { get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.OpenId); } init { } }
  23. /// <summary>
  24. /// Id of current tenant or null for host
  25. /// </summary>
  26. public string? UserId
  27. {
  28. get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
  29. init { }
  30. }
  31. /// <summary>
  32. /// Id of current user or throw Exception for guest
  33. /// </summary>
  34. /// <exception cref="AuthenticationException"></exception>
  35. public string RequiredUserId => _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
  36. public string? UserName
  37. {
  38. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
  39. init { }
  40. }
  41. public string? Phone
  42. {
  43. get { return _contextAccessor.HttpContext.User.FindFirstValue(JwtClaimTypes.PhoneNumber); }
  44. init { }
  45. }
  46. /// <summary>
  47. /// Roles
  48. /// </summary>
  49. public string[] Roles
  50. {
  51. get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == ClaimTypes.Role).Select(d => d.Value).ToArray(); }
  52. init { }
  53. }
  54. public string? OrgId
  55. {
  56. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
  57. init { }
  58. }
  59. public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
  60. public string? OrgName
  61. {
  62. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
  63. init { }
  64. }
  65. public int OrgLevel
  66. {
  67. get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
  68. init { }
  69. }
  70. public string? OrgAreaCode
  71. {
  72. get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.DepartmentAreaCode); }
  73. init { }
  74. }
  75. public bool OrgIsCenter
  76. {
  77. get { return _contextAccessor.HttpContext.User.FindBoolValue(AppClaimTypes.DepartmentIsCenter); }
  78. init { }
  79. }
  80. /// <summary>
  81. /// 部门行政区划名称
  82. /// </summary>
  83. public string? OrgAreaName
  84. {
  85. get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.DepartmentAreaName); }
  86. init { }
  87. }
  88. public string? AreaId
  89. {
  90. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
  91. init { }
  92. }
  93. public string? ClientId
  94. {
  95. get { return _contextAccessor.HttpContext.User.FindFirstValue(JwtClaimTypes.ClientId); }
  96. init { }
  97. }
  98. /// <summary>
  99. /// 工号
  100. /// </summary>
  101. public string? StaffNo
  102. {
  103. get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.StaffNo); }
  104. init { }
  105. }
  106. }