TestSessionContext.cs 4.0 KB

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