JWT加密解密方法
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加密解密方法的更多相关文章
- vb.net加密解密方法
1.vb.net加密解密方法 Private Function getLicenseDate() As String Dim b() As Byte Dim path As String = Serv ...
- ASP.NET常用加密解密方法
ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码 public static string ToMd5(string clearString) ...
- ios常见加密解密方法
在其他平台中经常会计算MD5值,在iOS平台中也提供了该方法,首先需要导入头文件 #import <CommonCrypto/CommonDigest.h> 方法CC_MD5可以获取MD5 ...
- C#/IOS/Android通用加密解密方法
原文:C#/IOS/Android通用加密解密方法 公司在做移动端ios/android,服务器提供接口使用的.net,用到加密解密这一块,也在网上找了一些方法,有些是.net加密了android解密 ...
- 2019-2-20C#开发中常用加密解密方法解析
C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...
- Java实现一个简单的加密解密方法
Crypto是Java语言写的一个简单的加密解密方法. 使用方法: 加密方法 String cipherte=Enande.encrypt(content, pass): 解密方法 Enande.de ...
- C#开发中常用的加密解密方法
转载自:https://www.cnblogs.com/bj981/p/11203711.html C#开发中常用的加密解密方法 相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天 ...
- JWT加密解密
如何保证WebAPI的安全?1.JWT加密解密.token2.使用https传输协议.3.把用户所有请求的参数信息加上一个只有服务器端知道的secret,做个散列运算,然后到了服务器端,服务器端也做一 ...
- PHP加密解密方法
加密解密方法 //字符串解密加密 function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_l ...
随机推荐
- Win10 Nodejs搭建http-server注意点
下载安装,并用命令行查看版本:如果提示输入命令找不到等,可能是没有安装成功,或者是环境变量引起的: 如果在提示安装不成功可能是win10权限问题,最好使用管理员模式运行cmd,再用cmd命令打开安装文 ...
- 杭电多校HDU 6579 Operation (线性基 区间最大)题解
题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...
- gradle中的build script详解
目录 简介 project和task 一个例子 task详细讲解 task脚本 task依赖 动态task 默认task build script的外部依赖 gradle中的build script详 ...
- CSS hover box
CSS hover box transition 踩坑指南, display: none; 作为初始状态,不会产生动画效果,必须设置 height: 0; 或 width: 0; 来实现隐藏! tra ...
- 1GB === 1000MB & 1GB === 1024MB
1GB === 1000MB & 1GB === 1024MB 字节单位换算 1 Gigabyte = 1000 Megabytes 1 Gibibyte = 1024 Mebibytes 十 ...
- swiper & swiper slider
swiper & swiper slider mobile swiper https://idangero.us/swiper/ https://idangero.us/swiper/get- ...
- 智能货柜 & 技术原理 (动态视觉识别 + 重力感应)
智能货柜 & 技术原理 (动态视觉识别 + 重力感应) 智能货柜 拥有智能化.精细化运营模式的智能货柜成为代替无人货架继前进的方式. 相比无人货架来说,智能货柜的技术门槛更高,拥有 RFID. ...
- nasm astrcat函数 x86
xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...
- Techme INC解读基因魔剪,带来的是机遇还是风险?
10月7日,诺贝尔化学奖颁给了法国美国生物学家Jennifer Doudna和生物化学家Emmanuelle Charpentier,以表彰她们对新一代基因技术CRISPR的贡献,全网沸腾. CRIS ...
- 12.scikit-learn中的Scaler
import numpy as np from sklearn import datasets iris = datasets.load_iris() X = iris.data y = iris.t ...