• 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

  • 相关阅读:
    Android——Activity去除标题栏和状态栏
    Android——程序员的情怀——优化BaseAdapter
    Android——Android Sutido:[2]导入eclipse项目篇
    【Android开源项目分析】自定义圆形头像CircleImageView的使用和源码分析
    学佛略要
    Keystone, Start, Failed to Load Bson
    又梦见了你
    伦敦之旅
    无题
    Multiverse in Doctor Strange // Multiverse在《神秘博士》
  • 原文地址:https://www.cnblogs.com/OnlyDreams/p/7569759.html
Copyright © 2020-2023  润新知