123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Security.Authentication;
- using System.Security.Claims;
- using Hotline.Authentications;
- using IdentityModel;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.DependencyInjection;
- using XF.Domain.Authentications;
- using XF.Domain.Dependency;
- namespace Hotline.Tests.Controller;
- public class TestSessionContext : ISessionContext, IScopeDependency
- {
- private readonly IHttpContextAccessor _contextAccessor;
- private readonly ISessionContextProvider _changeSessionProvider;
- public TestSessionContext(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider)
- {
- _contextAccessor = httpContextAccessor;
- _changeSessionProvider = serviceProvider.GetService<ISessionContextProvider>();
- //_contextAccessor.HttpContext ??= new DefaultHttpContext();
- //if (httpContextAccessor.HttpContext == null) _contextAccessor.HttpContext = new DefaultHttpContext();
- }
- public void ChangeSession(string id)
- {
- _contextAccessor.HttpContext = _changeSessionProvider.ChangeSessionByUserId(id, _contextAccessor.HttpContext);
- }
- public async Task ChangeSessionAsync(string userId, CancellationToken cancellation)
- {
- throw new NotImplementedException();
- }
- public void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
- {
- throw new NotImplementedException();
- }
- public string? OpenId { get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.OpenId); } init { } }
- /// <summary>
- /// Id of current tenant or null for host
- /// </summary>
- public string? UserId
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
- init { }
- }
- /// <summary>
- /// Id of current user or throw Exception for guest
- /// </summary>
- /// <exception cref="AuthenticationException"></exception>
- public string RequiredUserId => _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
- public string? UserName
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
- init { }
- }
- public string? Phone
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.PhoneNumber); }
- init { }
- }
- /// <summary>
- /// Roles
- /// </summary>
- public string[] Roles
- {
- get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray(); }
- init { }
- }
- public string? OrgId
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
- init { }
- }
- public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
- public string? OrgName
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
- init { }
- }
- public int OrgLevel
- {
- get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
- init { }
- }
- public string? OrgAreaCode
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaCode); }
- init { }
- }
- public bool OrgIsCenter
- {
- get { return _contextAccessor.HttpContext?.User.FindBoolValue(AppClaimTypes.DepartmentIsCenter) ?? false; }
- init { }
- }
- /// <summary>
- /// 部门行政区划名称
- /// </summary>
- public string? OrgAreaName
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaName); }
- init { }
- }
- public string? AreaId
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
- init { }
- }
- public string? ClientId
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.ClientId); }
- init { }
- }
- /// <summary>
- /// 工号
- /// </summary>
- public string? StaffNo
- {
- get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.StaffNo); }
- init { }
- }
- }
|