jwt加密

string token = new JwtBuilder()
.WithAlgorithm(new HMACSHA256Algorithm()) // 使用算法
.WithSecret(HearingRehabilitation.BusinessLayer.Cfg.Secret) // 使用秘钥
.AddClaim("exp", DateTimeOffset.UtcNow.AddMonths(1).ToUnixTimeSeconds())
.AddClaim("claim2", "claim2-value")
.Build();
try
{
string json = new JwtBuilder()
.WithSecret(HearingRehabilitation.BusinessLayer.Cfg.Secret)
.MustVerifySignature()
.Decode(token);

Console.WriteLine(json);
}
catch (TokenExpiredException)
{
Console.WriteLine("token 已过期");
}
catch (SignatureVerificationException)
{
Console.WriteLine("token 签名无效");
}
return true;

posted @ 2021-01-14 17:31  咧嘴玩手机的猫  阅读(146)  评论(0编辑  收藏  举报