博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jwt-dotnet使用示例
阅读量:6833 次
发布时间:2019-06-26

本文共 2350 字,大约阅读时间需要 7 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using JWT;using JWT.Algorithms;using JWT.Serializers;namespace JWTConsole{    class Program    {        static void Main(string[] args)        {            var payload = new Dictionary
{ { "claim1", 0 }, { "claim2", "claim2-value" } }; var secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk"; IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var token = encoder.Encode(payload, secret); Console.WriteLine(token); IDateTimeProvider provider = new UtcDateTimeProvider(); IJwtValidator validator = new JwtValidator(serializer, provider); IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder); try { //IDateTimeProvider provider = new UtcDateTimeProvider(); //IJwtValidator validator = new JwtValidator(serializer, provider); //IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder); var json = decoder.Decode(token, secret, verify: true); Console.WriteLine(json); } catch (TokenExpiredException) { Console.WriteLine("Token has expired"); } catch (SignatureVerificationException) { Console.WriteLine("Token has invalid signature"); } var now = provider.GetNow().AddMinutes(1); var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds); payload = new Dictionary
{ { "exp", secondsSinceEpoch } }; token = encoder.Encode(payload, secret); var json0 = decoder.Decode(token, secret,verify: true); // TokenExpiredException Console.WriteLine(json0); } }}

转载于:https://www.cnblogs.com/OnlyDreams/p/7569759.html

你可能感兴趣的文章
java通过传送地址获取坐标
查看>>
10个Python练手小程序,学习python的很好的资料
查看>>
Linux终端快捷键
查看>>
乐观锁与悲观锁
查看>>
docker windows container的一些注意点
查看>>
拥抱博客园
查看>>
yum使用详细
查看>>
2.字符串
查看>>
Linux权限管理命令
查看>>
[转]关于strtok和strtok_r函数的深度研究
查看>>
ios-自定义点击状态栏滚回顶部
查看>>
Django现有模型增加字段
查看>>
解决IE6浏览器下position:fixed固定定位问题
查看>>
Rest分页接口开发
查看>>
Mybatis中oracle如何批量insert语句
查看>>
org.aspectj.lang.JoinPoint-中文简要API(转)
查看>>
面向对象知识
查看>>
数据库中算式的简单例子
查看>>
AVFoundation自定义拍照
查看>>
测试显示GitHub的Gist
查看>>