You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.6 KiB
42 lines
1.6 KiB
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using Znyc.CloudCar.Configuration;
|
|
using Znyc.CloudCar.Utility.Helper;
|
|
|
|
namespace Znyc.CloudCar.Auth.Policys
|
|
{
|
|
|
|
/// <summary>
|
|
/// JWTToken生成类
|
|
/// </summary>
|
|
public class JwtToken
|
|
{
|
|
public static string Create(Claim[] claims)
|
|
{
|
|
SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettingsConstVars.JwtConfigSecretKey));
|
|
SigningCredentials signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
string timestamp = DateTime.Now.AddMinutes(AppSettingsConstVars.JwtConfigExpires).ToTimestamp()
|
|
.ToString();
|
|
claims = claims.Append(new Claim(Claims.RefreshExpires, timestamp)).ToArray();
|
|
|
|
JwtSecurityToken token = new JwtSecurityToken(
|
|
AppSettingsConstVars.JwtConfigIssuer,
|
|
AppSettingsConstVars.JwtConfigAudience,
|
|
claims,
|
|
DateTime.Now,
|
|
DateTime.Now.AddMinutes(AppSettingsConstVars.JwtConfigExpires),
|
|
signingCredentials
|
|
);
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
|
|
public Claim[] Decode(string jwtToken)
|
|
{
|
|
JwtSecurityTokenHandler jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
|
|
JwtSecurityToken jwtSecurityToken = jwtSecurityTokenHandler.ReadJwtToken(jwtToken);
|
|
return jwtSecurityToken?.Claims?.ToArray();
|
|
}
|
|
}
|
|
}
|
|
|