public static string Key { get; set; } = "123456789987654321";//解密串
/// <summary>
/// 加密方法
/// </summary>
/// <param name="payload">需要加密的字典</param>
/// <param name="key"></param>
/// <returns></returns>
public static string Encoder(Dictionary<string, object> payload, string key = null)
{
if (string.IsNullOrEmpty(key))
{ key = Key;
} IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); //设置失效时间 payload.Add("timeout", DateTime.Now.AddMinutes(3)); return encoder.Encode(payload, key); } /// <summary>
/// 解密方法
/// </summary>
/// <param name="jwtStr"></param>
/// <param name="key"></param>
/// <returns></returns>
public static Dictionary<string, object> Decode(string jwtStr, string key = null)
{
if (string.IsNullOrEmpty(key))
{
key = Key;
}
try
{
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();//HMACSHA256加密 IJsonSerializer serializer = new JsonNetSerializer(); IDateTimeProvider provider = new UtcDateTimeProvider(); IJwtValidator validator = new JwtValidator(serializer, provider); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm); var json = decoder.Decode(jwtStr, key, true); //把字符串反向生成对应的对象类
var result = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
if ((DateTime)result["timeout"] < DateTime.Now)
{
result.Remove(key: "timeout");
throw new Exception(message: "jwt已经过期,请重新登陆");
} return result;
}
catch (TokenExpiredException)
{ throw new Exception(message: "Token has expired"); }
catch (SignatureVerificationException)
{ throw new Exception(message: "Token has invalid signature"); } } public static string CheckHeardTocken(HttpRequest request)
{
if (request.Headers["token"]==null)
{
throw new Exception("请登录");
}
return Newtonsoft.Json.JsonConvert.SerializeObject( Decode(request.Headers["token"]));
}

  

JWT加密解密方法的更多相关文章

  1. vb.net加密解密方法

    1.vb.net加密解密方法 Private Function getLicenseDate() As String Dim b() As Byte Dim path As String = Serv ...

  2. ASP.NET常用加密解密方法

    ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码           public static string ToMd5(string clearString)        ...

  3. ios常见加密解密方法

    在其他平台中经常会计算MD5值,在iOS平台中也提供了该方法,首先需要导入头文件 #import <CommonCrypto/CommonDigest.h> 方法CC_MD5可以获取MD5 ...

  4. C#/IOS/Android通用加密解密方法

    原文:C#/IOS/Android通用加密解密方法 公司在做移动端ios/android,服务器提供接口使用的.net,用到加密解密这一块,也在网上找了一些方法,有些是.net加密了android解密 ...

  5. 2019-2-20C#开发中常用加密解密方法解析

    C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...

  6. Java实现一个简单的加密解密方法

    Crypto是Java语言写的一个简单的加密解密方法. 使用方法: 加密方法 String cipherte=Enande.encrypt(content, pass): 解密方法 Enande.de ...

  7. C#开发中常用的加密解密方法

    转载自:https://www.cnblogs.com/bj981/p/11203711.html C#开发中常用的加密解密方法 相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天 ...

  8. JWT加密解密

    如何保证WebAPI的安全?1.JWT加密解密.token2.使用https传输协议.3.把用户所有请求的参数信息加上一个只有服务器端知道的secret,做个散列运算,然后到了服务器端,服务器端也做一 ...

  9. PHP加密解密方法

    加密解密方法 //字符串解密加密 function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_l ...

随机推荐

  1. free video tutorial of Deep Learning

    free video tutorial of Deep Learning AI 深度学习/ 机器学习/人工智能 Deep Learning With Deep Learning – a form of ...

  2. React Hooks: useMemo All In One

    React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...

  3. JS Object Deep Copy & 深拷贝

    JS Object Deep Copy & 深拷贝 针对深度拷贝,需要使用其他方法 JSON.parse(JSON.stringify(obj));,因为 Object.assign() 拷贝 ...

  4. GraphQL All In One

    GraphQL All In One refs https://github.com/hasura/learn-graphql xgqfrms 2012-2020 www.cnblogs.com 发布 ...

  5. React LifeCycle API

    React LifeCycle API old API & new API 不可以混用 demo https://codesandbox.io/s/react-parent-child-lif ...

  6. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

  7. svg & regex

    svg & regex https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reg ...

  8. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  9. NGK:价值对标比特币,上线暴涨4558%,下一个财富暴增风口

    近期,美股行情多变,一直饱受争议的比特币也成了其中的"弄潮儿".看多者认为,机构的兴趣有助于支撑比特币作为对冲美元疲软和通胀的工具. 特别是今年1月底的时候,马斯克将推特简介更改为 ...

  10. JUC并发集合类CopyOnWriteList

    CopyOnWriteList简介 ArrayList是线程不安全的,于是JDK新增加了一个线程并发安全的List--CopyOnWriteList,中心思想就是copy-on-write,简单来说是 ...