StableVersion4.3/HL_FristAidPlatform_Token/JwtHelper.cs

102 lines
4.3 KiB
C#

using HL_FristAidPlatform_Help;
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 HL_FristAidPlatform_Token
{
public class JwtHelper
{
/// <summary>
/// 颁发JWT字符串
/// </summary>
/// <param name="tokenModel"></param>
/// <returns></returns>
public static string GetJWT(TokenModel tokenModel)
{
var c = new List<Claim>();
c.Add(new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid.ToString()));
c.Add(new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"));
c.Add(new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"));
c.Add(new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddMinutes(60)).ToUnixTimeSeconds()}"));
c.Add(new Claim(JwtRegisteredClaimNames.Iss, "API"));
c.Add(new Claim(JwtRegisteredClaimNames.Aud, "wr"));
if (tokenModel.Role != null && tokenModel.Role.Length > 0)
{
foreach (var item in tokenModel.Role)
{
if (!string.IsNullOrEmpty(item))
{
c.Add(new Claim(ClaimTypes.Role, item));
}
}
}
//秘钥
var jwtConfig = new JwtAuthConfigModel();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.JWTSecretKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var jwt = new JwtSecurityToken(
claims: c, //声明集合
signingCredentials: creds);
var jwtHandler = new JwtSecurityTokenHandler().WriteToken(jwt);
//var newJwt = SerializeJWT(jwtHandler);
return jwtHandler;
}
/// <summary>
/// 更新令牌
/// </summary>
/// <param name="jwtStr"></param>
/// <returns></returns>
public static string ResetJWT(string OldjwtStr, string deviceIP)
{
string tokenNew = "";
try
{
//SecurityToken validatedToken;
//var claim = new JwtSecurityTokenHandler().ValidateToken(OldjwtStr, validatedToken);
JwtSecurityToken jwtToken = new JwtSecurityTokenHandler().ReadJwtToken(OldjwtStr);
object jti;
jwtToken.Payload.TryGetValue("jti", out jti);
long currentTime = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds();
if (deviceIP.ToString() == jti.ToString())
{
var temp = jwtToken.Claims;
var claims = new List<Claim>();
claims.AddRange(temp.Where(t => t.Type != JwtRegisteredClaimNames.Iat));
//重置token的发布时间为当前时间
claims.Add(new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"));
//var now = $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}";
var now = DateTime.Now;
//秘钥
var jwtConfig = new JwtAuthConfigModel();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.JWTSecretKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var jwtSecurityToken = new JwtSecurityToken(
claims: claims,
notBefore: now,
expires: now.AddDays(1),
signingCredentials: creds
);
tokenNew = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
}
else
{
tokenNew = "你的令牌错误!";
}
}
catch (Exception e)
{
Help.Debug("在类HL_FristAidPlatform_Token中的方法ResetJWT出现错误:" + e);
}
return tokenNew;
}
}
}