You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.6 KiB
C#

12 months ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace DS.Module.UserModule;
public class AspNetUser : IUser
{
private readonly IHttpContextAccessor _accessor;
public AspNetUser(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public UserInfo UserInfo => GetUserInfo();
public string GetToken()
{
return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
}
public string UserId => GetClaimValueByType("jti").FirstOrDefault().ObjToString();
public long GetTenantId()
12 months ago
{
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
var tenantId = String.Empty;
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Cookies["Token"];
// token校验
var jwtHandler = new JwtSecurityTokenHandler();
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
{
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
tenantId = jwtToken.Claims.First(x => x.Type == "TenantId").Value;
}
10 months ago
return Convert.ToInt64(tenantId);
12 months ago
}
10 months ago
public long GetOrgId()
12 months ago
{
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
10 months ago
var orgId = String.Empty;
12 months ago
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Cookies["Token"];
// token校验
var jwtHandler = new JwtSecurityTokenHandler();
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
{
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
10 months ago
orgId = jwtToken.Claims.First(x => x.Type == "OrgId").Value;
12 months ago
}
10 months ago
return Convert.ToInt64(orgId);
12 months ago
}
10 months ago
public string GetCompanyId()
12 months ago
{
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
10 months ago
var companyId = String.Empty;
12 months ago
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
?? _accessor.HttpContext?.Request.Cookies["Token"];
// token校验
var jwtHandler = new JwtSecurityTokenHandler();
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
{
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
10 months ago
companyId = jwtToken.Claims.First(x => x.Type == "OrgId").Value;
12 months ago
}
10 months ago
return companyId;
12 months ago
}
10 months ago
12 months ago
public string TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToString();
public string CompanyId => GetClaimValueByType("CompanyId").FirstOrDefault().ObjToString();
10 months ago
public string OrgId => GetClaimValueByType("OrgId").FirstOrDefault().ObjToString();
12 months ago
public UserInfo GetUserInfo()
{
if (_accessor.HttpContext.User.Claims == null)
{
return null;
}
var json = _accessor.HttpContext.User.Claims;
//获取上传token可自定义扩展
var token = _accessor.HttpContext.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
?? _accessor.HttpContext.Request.Headers["X-Token"].FirstOrDefault()
?? _accessor.HttpContext.Request.Query["Token"].FirstOrDefault()
?? _accessor.HttpContext.Request.Cookies["Token"];
// token校验
var jwtHandler = new JwtSecurityTokenHandler();
var userInfo = new UserInfo();
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
{
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
var UserId = jwtToken.Claims.First().Value;
// userInfo = db.Queryable<SysUser>()
// .Where(a =>
// a.Deleted == false && a.Id == user.Id)
// .Select(a => new UserInfo
// {
// UserId = a.Id, UserCode = a.UserCode, UserName = a.NickName,
10 months ago
// // OrgId = a.OrgId.ToString(), CompanyName = a.CustomerName
12 months ago
// }).First();
// var t1 = JsonConvert.DeserializeObject<UserInfo>(temp);
userInfo = JsonConvert.DeserializeObject<UserInfo>(jwtToken.Claims.First().Value);
}
return userInfo;
}
public IEnumerable<Claim> GetClaimsIdentity()
{
var claims = _accessor.HttpContext.User.Claims.ToList();
var headers = _accessor.HttpContext.Request.Headers;
foreach (var header in headers)
{
claims.Add(new Claim(header.Key, header.Value));
}
return claims;
}
public List<string> GetClaimValueByType(string ClaimType)
{
return (from item in GetClaimsIdentity()
where item.Type == ClaimType
select item.Value).ToList();
}
}