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.

141 lines
5.5 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 string GetTenantId()
{
// 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;
}
return tenantId;
}
public string GetCompanyId()
{
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
var companyId = 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);
companyId = jwtToken.Claims.First(x => x.Type == "CompanyId").Value;
}
return companyId;
}
public string GetUserGID()
{
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
var GID = 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);
GID = jwtToken.Claims.First(x => x.Type == "GID").Value;
}
return GID;
}
public string TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToString();
public string CompanyId => GetClaimValueByType("CompanyId").FirstOrDefault().ObjToString();
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,
// // CompanyId = a.CompanyId.ToString(), CompanyName = a.CustomerName
// }).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();
}
}