zoukankan      html  css  js  c++  java
  • jwt-dotnet使用示例

    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<string, object>{
                    { "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<string, object>{
                        { "exp", secondsSinceEpoch }
                };
                token = encoder.Encode(payload, secret);
                var json0 = decoder.Decode(token, secret,verify: true); // TokenExpiredException
                Console.WriteLine(json0);
    
            }
        }
    }

    https://github.com/jwt-dotnet/jwt

  • 相关阅读:
    关于grunt
    关于网页上标题图标显示
    form表单原理
    js判断是android访问还是ios访问
    判断客户端是手机访问还是电脑访问网站(php代码)
    电脑手机模拟器模拟手机浏览器,在线浏览手机网站
    手机网站通过JS判断是否为iPhone手机访问
    手机页面一键拨号
    html5手机网站常用的9个CSS属性
    js解析与序列化json数据(一)json.stringify()的基本用法
  • 原文地址:https://www.cnblogs.com/OnlyDreams/p/7569759.html
Copyright © 2011-2022 走看看