|
|
|
|
using EntrustSettle.Common.Const;
|
|
|
|
|
using EntrustSettle.Common.Swagger;
|
|
|
|
|
using EntrustSettle.Model;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
|
|
|
namespace EntrustSettle.Common.HttpContextUser
|
|
|
|
|
{
|
|
|
|
|
public class DJYUser : IUser
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpContextAccessor _accessor;
|
|
|
|
|
|
|
|
|
|
public DJYUser(IHttpContextAccessor accessor)
|
|
|
|
|
{
|
|
|
|
|
_accessor = accessor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ID => GetClaimValueByType(ClaimConst.LOGIN_ID).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
public string Name => GetClaimValueByType(ClaimConst.LOGIN_NAME).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
public string CompanyId => GetClaimValueByType(ClaimConst.LOGIN_COMPANYID).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
public string CompanyName => GetClaimValueByType(ClaimConst.LOGIN_COMPANYNAME).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
public string Mobile => GetClaimValueByType(ClaimConst.LOGIN_MOBILE).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
public bool IsAuthenticated()
|
|
|
|
|
{
|
|
|
|
|
return _accessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetToken()
|
|
|
|
|
{
|
|
|
|
|
var token = _accessor.HttpContext?.Request?.Headers["Authorization"].ObjToString().Replace("Bearer ", "");
|
|
|
|
|
if (!token.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_accessor.HttpContext?.IsSuccessSwagger() == true)
|
|
|
|
|
{
|
|
|
|
|
token = _accessor.HttpContext.GetSuccessSwaggerJwt();
|
|
|
|
|
if (token.IsNotEmptyOrNull())
|
|
|
|
|
{
|
|
|
|
|
if (_accessor.HttpContext.User.Claims.Any(s => s.Type == JwtRegisteredClaimNames.Jti))
|
|
|
|
|
{
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var claims = new ClaimsIdentity(GetClaimsIdentity(token));
|
|
|
|
|
_accessor.HttpContext.User.AddIdentity(claims);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> GetUserInfoFromToken(string ClaimType)
|
|
|
|
|
{
|
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
|
var token = "";
|
|
|
|
|
|
|
|
|
|
token = GetToken();
|
|
|
|
|
// token校验
|
|
|
|
|
if (token.IsNotEmptyOrNull() && jwtHandler.CanReadToken(token))
|
|
|
|
|
{
|
|
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
|
|
|
|
|
|
return (from item in jwtToken.Claims
|
|
|
|
|
where item.Type == ClaimType
|
|
|
|
|
select item.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<string>() { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageModel<string> MessageModel { get; set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Claim> GetClaimsIdentity()
|
|
|
|
|
{
|
|
|
|
|
if (_accessor.HttpContext == null) return ArraySegment<Claim>.Empty;
|
|
|
|
|
|
|
|
|
|
if (!IsAuthenticated()) return GetClaimsIdentity(GetToken());
|
|
|
|
|
|
|
|
|
|
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 IEnumerable<Claim> GetClaimsIdentity(string token)
|
|
|
|
|
{
|
|
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
|
|
// token校验
|
|
|
|
|
if (token.IsNotEmptyOrNull() && jwtHandler.CanReadToken(token))
|
|
|
|
|
{
|
|
|
|
|
var jwtToken = jwtHandler.ReadJwtToken(token);
|
|
|
|
|
|
|
|
|
|
return jwtToken.Claims;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<Claim>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> GetClaimValueByType(string ClaimType)
|
|
|
|
|
{
|
|
|
|
|
return (from item in GetClaimsIdentity()
|
|
|
|
|
where item.Type == ClaimType
|
|
|
|
|
select item.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//适配项目
|
|
|
|
|
//public long ID => GetClaimValueByType("loginid").FirstOrDefault().ObjToLong();
|
|
|
|
|
//public long TenantId => GetClaimValueByType("TenantId").FirstOrDefault().ObjToLong();
|
|
|
|
|
}
|
|
|
|
|
}
|