126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
|
|||
|
|
using Microsoft.Extensions.Options;
|
||
|
|
using Microsoft.IdentityModel.Tokens;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IdentityModel.Tokens.Jwt;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Security.Claims;
|
||
|
|
using System.Text;
|
||
|
|
|
||
|
|
namespace JWTHelper
|
||
|
|
{
|
||
|
|
public class JwtTools
|
||
|
|
{
|
||
|
|
private JWTConfig config;
|
||
|
|
|
||
|
|
public JwtTools(IOptions<JWTConfig> opts)
|
||
|
|
{
|
||
|
|
config = opts.Value;
|
||
|
|
}
|
||
|
|
public bool ValidateToken(string token)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.SecretKey));
|
||
|
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||
|
|
|
||
|
|
var tokenValidationParameters = new TokenValidationParameters();
|
||
|
|
|
||
|
|
tokenValidationParameters.ValidateAudience = config.ValidateAudience;
|
||
|
|
tokenValidationParameters.ValidAudience = config.Audience;
|
||
|
|
|
||
|
|
tokenValidationParameters.ValidateIssuer = config.ValidateIssuer;
|
||
|
|
tokenValidationParameters.ValidIssuer = config.Issuer;
|
||
|
|
|
||
|
|
tokenValidationParameters.ValidateIssuerSigningKey = config.ValidateIssuerSigningKey;
|
||
|
|
tokenValidationParameters.IssuerSigningKey =
|
||
|
|
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.SecretKey));
|
||
|
|
|
||
|
|
tokenValidationParameters.ValidateLifetime = config.ValidateLifetime;
|
||
|
|
SecurityToken securityToken = null;
|
||
|
|
var identiy = new JwtSecurityTokenHandler().ValidateToken(token, tokenValidationParameters, out securityToken);
|
||
|
|
return identiy.Claims.Count(n => n.Type == "uid" || n.Type == "uname") >= 2;
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public string CreateToken(Claim[] claims)
|
||
|
|
{
|
||
|
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.SecretKey));
|
||
|
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||
|
|
var token = new JwtSecurityToken(
|
||
|
|
issuer: config.Issuer,
|
||
|
|
// audience: jwtconfig.Audience,
|
||
|
|
claims: claims,
|
||
|
|
expires: DateTime.Now.AddMinutes(60),
|
||
|
|
signingCredentials: creds
|
||
|
|
);
|
||
|
|
|
||
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public static string GetTokenContent(string token, string key)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrEmpty(token))
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var securityTokenHandler = new JwtSecurityTokenHandler();
|
||
|
|
|
||
|
|
var claims = securityTokenHandler.ReadJwtToken(token).Payload;
|
||
|
|
|
||
|
|
return claims.Claims.FirstOrDefault(n => n.Type == key)?.Value ?? "";
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string GetTokenReverseContent(string token, string key)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrEmpty(token))
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var securityTokenHandler = new JwtSecurityTokenHandler();
|
||
|
|
|
||
|
|
var claims = securityTokenHandler.ReadJwtToken(token).Payload;
|
||
|
|
|
||
|
|
return new string((claims.Claims.FirstOrDefault(n => n.Type == key)?.Value ?? "").ToCharArray()
|
||
|
|
.AsEnumerable().Reverse().ToArray());
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static IEnumerable<Claim> GetTokenClaim(string token)
|
||
|
|
{
|
||
|
|
var securityTokenHandler = new JwtSecurityTokenHandler();
|
||
|
|
|
||
|
|
var claims = securityTokenHandler.ReadJwtToken(token).Payload;
|
||
|
|
|
||
|
|
return claims.Claims;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|