DefaultHttpContextAccessor.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Hotline.Application.Tests.Infrastructure;
  2. using Hotline.Identity.Accounts;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Security.Authentication;
  8. using System.Security.Claims;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using XF.Domain.Authentications;
  12. using XF.Domain.Dependency;
  13. using XF.Domain.Repository;
  14. namespace Hotline.Application.Tests.Controller;
  15. public class DefaultHttpContextAccessor : ISessionContext, IScopeDependency
  16. {
  17. private HttpContext _content = new DefaultHttpContext();
  18. private HttpContext GetContext()
  19. {
  20. var context = new DefaultHttpContext();
  21. //var openId = new Claim(AppClaimTypes.OpenId, "测试生成的OpenId");
  22. var id = new ClaimsIdentity("身份");
  23. //id.AddClaim(openId);
  24. context.User = new ClaimsPrincipal(id);
  25. //OpenId = context.User.FindFirstValue(AppClaimTypes.OpenId);
  26. return context;
  27. }
  28. public HttpContext? HttpContext { get => GetContext(); set => _content = value; }
  29. public string? OpenId { get { return TestSessionConstants.OpenId; } set { } }
  30. /// <summary>
  31. /// Id of current tenant or null for host
  32. /// </summary>
  33. public string? UserId
  34. {
  35. get
  36. {
  37. return TestSessionConstants.UserId;
  38. }
  39. init { }
  40. }
  41. /// <summary>
  42. /// Id of current user or throw Exception for guest
  43. /// </summary>
  44. /// <exception cref="AuthenticationException"></exception>
  45. public string RequiredUserId { get { return TestSessionConstants.UserId; } }
  46. public string? UserName { get { return TestSessionConstants.UserName; } init { } }
  47. public string? Phone { get; init; }
  48. /// <summary>
  49. /// Roles
  50. /// </summary>
  51. public string[] Roles { get { return TestSessionConstants.Roles; } init { } }
  52. public string? OrgId { get { return TestSessionConstants.OrgId; } init { } }
  53. public string RequiredOrgId { get { return TestSessionConstants.OrgId; } }
  54. public string? OrgName { get; init; }
  55. public int OrgLevel { get; init; }
  56. public string? OrgAreaCode { get; init; }
  57. public bool OrgIsCenter { get; init; }
  58. /// <summary>
  59. /// 部门行政区划名称
  60. /// </summary>
  61. public string? OrgAreaName { get; init; }
  62. public string? AreaId { get; init; }
  63. public string? ClientId { get; init; }
  64. /// <summary>
  65. /// 工号
  66. /// </summary>
  67. public string? StaffNo { get; init; }
  68. }