|
|
@ -18,127 +18,386 @@ public class AspNetUser : IUser
|
|
|
|
|
|
|
|
|
|
|
|
public UserInfo UserInfo => GetUserInfo();
|
|
|
|
public UserInfo UserInfo => GetUserInfo();
|
|
|
|
|
|
|
|
|
|
|
|
public string GetToken()
|
|
|
|
//public string GetToken()
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public string UserId => GetClaimValueByType("jti").FirstOrDefault().ObjToString();
|
|
|
|
|
|
|
|
private string _userId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string UserId
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
if (_userId == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var claimValue = GetClaimValueByType("jti").FirstOrDefault();
|
|
|
|
|
|
|
|
_userId = claimValue != null ? claimValue.ObjToString() : "东胜软件";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _userId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_userId = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string UserId => GetClaimValueByType("jti").FirstOrDefault().ObjToString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public long GetTenantId()
|
|
|
|
public long GetTenantId()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
var token = GetToken();
|
|
|
|
var tenantId = String.Empty;
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
|
|
|
|
{
|
|
|
|
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
|
|
|
|
return 0;
|
|
|
|
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
|
|
|
|
}
|
|
|
|
?? _accessor.HttpContext?.Request.Cookies["Token"];
|
|
|
|
|
|
|
|
// token校验
|
|
|
|
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
|
|
|
|
if (!jwtHandler.CanReadToken(token))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
|
|
|
|
var tenantIdClaim = jwtToken.Claims.FirstOrDefault(x => x.Type == "TenantId");
|
|
|
|
|
|
|
|
|
|
|
|
tenantId = jwtToken.Claims.First(x => x.Type == "TenantId").Value;
|
|
|
|
return tenantIdClaim != null ? Convert.ToInt64(tenantIdClaim.Value) : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Convert.ToInt64(tenantId);
|
|
|
|
//public long 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 Convert.ToInt64(tenantId);
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
public long GetOrgId()
|
|
|
|
public long GetOrgId()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
var token = GetToken();
|
|
|
|
var orgId = String.Empty;
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
|
|
|
|
{
|
|
|
|
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
|
|
|
|
return 0;
|
|
|
|
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
|
|
|
|
}
|
|
|
|
?? _accessor.HttpContext?.Request.Cookies["Token"];
|
|
|
|
|
|
|
|
// token校验
|
|
|
|
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
|
|
|
|
if (!jwtHandler.CanReadToken(token))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
|
|
|
|
var orgIdClaim = jwtToken.Claims.FirstOrDefault(x => x.Type == "OrgId");
|
|
|
|
|
|
|
|
|
|
|
|
orgId = jwtToken.Claims.First(x => x.Type == "OrgId").Value;
|
|
|
|
return orgIdClaim != null ? Convert.ToInt64(orgIdClaim.Value) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Convert.ToInt64(orgId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public long GetOrgId()
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// // return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
|
|
|
|
// var orgId = 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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// orgId = jwtToken.Claims.First(x => x.Type == "OrgId").Value;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return Convert.ToInt64(orgId);
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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 == "OrgId").Value;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return companyId;
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetCompanyId()
|
|
|
|
public string GetCompanyId()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
var token = GetToken();
|
|
|
|
var companyId = String.Empty;
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
var token = _accessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
|
|
|
|
{
|
|
|
|
?? _accessor.HttpContext?.Request.Headers["X-Token"].FirstOrDefault()
|
|
|
|
return string.Empty;
|
|
|
|
?? _accessor.HttpContext?.Request.Query["Token"].FirstOrDefault()
|
|
|
|
}
|
|
|
|
?? _accessor.HttpContext?.Request.Cookies["Token"];
|
|
|
|
|
|
|
|
// token校验
|
|
|
|
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
|
|
|
|
if (!jwtHandler.CanReadToken(token))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
|
|
|
|
var companyIdClaim = jwtToken.Claims.FirstOrDefault(x => x.Type == "OrgId");
|
|
|
|
|
|
|
|
|
|
|
|
companyId = jwtToken.Claims.First(x => x.Type == "OrgId").Value;
|
|
|
|
return companyIdClaim?.Value ?? "0000000001";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public string TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToString();
|
|
|
|
|
|
|
|
private string _tenantId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string TenantId
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_tenantId == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var tenantIdClaim = GetClaimValueByType("TenantId").FirstOrDefault();
|
|
|
|
|
|
|
|
_tenantId = tenantIdClaim != null ? tenantIdClaim.ObjToString() : "00000000000001";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _tenantId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_tenantId = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return companyId;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public string CompanyId => GetClaimValueByType("CompanyId").FirstOrDefault().ObjToString();
|
|
|
|
|
|
|
|
private string _companyId;
|
|
|
|
|
|
|
|
|
|
|
|
public string TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToString();
|
|
|
|
public string CompanyId
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_companyId == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var companyIdClaim = GetClaimValueByType("CompanyId").FirstOrDefault();
|
|
|
|
|
|
|
|
_companyId = companyIdClaim != null ? companyIdClaim.ObjToString() : "东胜软件";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _companyId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_companyId = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string CompanyId => GetClaimValueByType("CompanyId").FirstOrDefault().ObjToString();
|
|
|
|
// public string OrgId => GetClaimValueByType("OrgId").FirstOrDefault().ObjToString();
|
|
|
|
|
|
|
|
private string _orgId;
|
|
|
|
|
|
|
|
|
|
|
|
public string OrgId => GetClaimValueByType("OrgId").FirstOrDefault().ObjToString();
|
|
|
|
public string OrgId
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_orgId == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var orgIdClaim = GetClaimValueByType("OrgId").FirstOrDefault();
|
|
|
|
|
|
|
|
_orgId = orgIdClaim != null ? orgIdClaim.ObjToString() : "1111111111";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _orgId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_orgId = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UserInfo GetUserInfo()
|
|
|
|
public UserInfo GetUserInfo()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_accessor.HttpContext.User.Claims == null)
|
|
|
|
var user = _accessor.HttpContext?.User;
|
|
|
|
|
|
|
|
if (user == null || !user.Claims.Any())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
return GetDefaultUserInfo();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var token = GetToken();
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return GetDefaultUserInfo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
var userInfo = new UserInfo();
|
|
|
|
if (!jwtHandler.CanReadToken(token))
|
|
|
|
if (!token.IsNullOrEmpty() && jwtHandler.CanReadToken(token))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
return GetDefaultUserInfo();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
var UserId = jwtToken.Claims.First().Value;
|
|
|
|
var userInfoJson = jwtToken.Claims.First().Value;
|
|
|
|
// userInfo = db.Queryable<SysUser>()
|
|
|
|
var userInfo = JsonConvert.DeserializeObject<UserInfo>(userInfoJson);
|
|
|
|
// .Where(a =>
|
|
|
|
|
|
|
|
// a.Deleted == false && a.Id == user.Id)
|
|
|
|
return userInfo ?? GetDefaultUserInfo();
|
|
|
|
// .Select(a => new UserInfo
|
|
|
|
}
|
|
|
|
// {
|
|
|
|
|
|
|
|
// UserId = a.Id, UserCode = a.UserCode, UserName = a.NickName,
|
|
|
|
private UserInfo GetDefaultUserInfo()
|
|
|
|
// // OrgId = a.OrgId.ToString(), CompanyName = a.CustomerName
|
|
|
|
{
|
|
|
|
// }).First();
|
|
|
|
return new UserInfo()
|
|
|
|
// var t1 = JsonConvert.DeserializeObject<UserInfo>(temp);
|
|
|
|
{
|
|
|
|
userInfo = JsonConvert.DeserializeObject<UserInfo>(jwtToken.Claims.First().Value);
|
|
|
|
CompanyId = "000001",
|
|
|
|
|
|
|
|
CompanyName = "东胜软件",
|
|
|
|
|
|
|
|
Token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiIsImFkbWluIjp0cnVlfQ",
|
|
|
|
|
|
|
|
UserCode = "0000001",
|
|
|
|
|
|
|
|
UserId = "00000000",
|
|
|
|
|
|
|
|
UserName = "东胜软件",
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetToken()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_accessor == null || _accessor.HttpContext == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return "东胜软件";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var request = _accessor.HttpContext.Request;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var authorizationHeader = request.Headers["Authorization"].FirstOrDefault();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(authorizationHeader))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return authorizationHeader.Split(" ").Last();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return userInfo;
|
|
|
|
return request.Headers["X-Token"].FirstOrDefault()
|
|
|
|
|
|
|
|
?? request.Query["Token"].FirstOrDefault()
|
|
|
|
|
|
|
|
?? request.Cookies["Token"]
|
|
|
|
|
|
|
|
?? "东胜软件";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 优化前
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public string GetToken()
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// var headers = _accessor.HttpContext?.Request?.Headers;
|
|
|
|
|
|
|
|
// if (headers != null && headers.ContainsKey("Authorization"))
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// return headers["Authorization"].ToString().Replace("Bearer ", "");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//public UserInfo GetUserInfo()
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// if (_accessor.HttpContext.User.Claims == null)
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// return new UserInfo()
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// CompanyId = "1",
|
|
|
|
|
|
|
|
// CompanyName = "初始化",
|
|
|
|
|
|
|
|
// Token = "初始化",
|
|
|
|
|
|
|
|
// UserCode = "初始化",
|
|
|
|
|
|
|
|
// UserId = "00000000",
|
|
|
|
|
|
|
|
// UserName = "初始化",
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// //var json = _accessor.HttpContext.User.Claims;
|
|
|
|
|
|
|
|
// //获取上传token,可自定义扩展
|
|
|
|
|
|
|
|
// var httphedad = _accessor.HttpContext.Request;
|
|
|
|
|
|
|
|
// var token = httphedad.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last()
|
|
|
|
|
|
|
|
// ?? httphedad.Headers["X-Token"].FirstOrDefault()
|
|
|
|
|
|
|
|
// ?? httphedad.Query["Token"].FirstOrDefault()
|
|
|
|
|
|
|
|
// ?? httphedad.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,
|
|
|
|
|
|
|
|
// // // OrgId = a.OrgId.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;
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion 优化前
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 保持在请求上下文中的所有声明 不为空
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
public IEnumerable<Claim> GetClaimsIdentity()
|
|
|
|
public IEnumerable<Claim> GetClaimsIdentity()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var claims = _accessor.HttpContext.User.Claims.ToList();
|
|
|
|
var claims = new List<Claim>();
|
|
|
|
var headers = _accessor.HttpContext.Request.Headers;
|
|
|
|
var user = _accessor.HttpContext?.User;
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
claims.AddRange(user.Claims);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var headers = _accessor.HttpContext?.Request?.Headers;
|
|
|
|
|
|
|
|
if (headers != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
foreach (var header in headers)
|
|
|
|
foreach (var header in headers)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
claims.Add(new Claim(header.Key, header.Value));
|
|
|
|
foreach (var value in header.Value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(value))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
claims.Add(new Claim(header.Key, value));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return claims;
|
|
|
|
return claims;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 键值对获取相应的内容 如果内容不存在则返回默认值 GetClaimValueByType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 键值对获取相应的内容 如果内容不存在则返回默认值
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="ClaimType"></param>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
public List<string> GetClaimValueByType(string ClaimType)
|
|
|
|
public List<string> GetClaimValueByType(string ClaimType)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return (from item in GetClaimsIdentity()
|
|
|
|
var claimsIdentity = GetClaimsIdentity();
|
|
|
|
where item.Type == ClaimType
|
|
|
|
if (claimsIdentity == null || !claimsIdentity.Any(item => item.Type == ClaimType))
|
|
|
|
select item.Value).ToList();
|
|
|
|
{
|
|
|
|
|
|
|
|
return new List<string> { "jti", "TenantId", "CompanyId", "OrgId" }; // 返回包含默认参数的列表
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return claimsIdentity.Where(item => item.Type == ClaimType).Select(item => item.Value).ToList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion 键值对获取相应的内容 如果内容不存在则返回默认值 GetClaimValueByType
|
|
|
|
}
|
|
|
|
}
|