zoukankan      html  css  js  c++  java
  • JWT生成token

    using JWT;
    using JWT.Algorithms;
    using JWT.Exceptions;
    using JWT.Serializers;
    using System;
    using System.Collections.Generic;

    namespace common
    {
    public class JWTHelper
    {
    public static string CreateJWT()
    {
    var payload = new Dictionary<string, object>
    {
    { "claim1", 0 },
    { "claim2", "claim2-value" }
    };
    const string 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);
    return token;
    }
    public static string DecodeJWT(string token)
    {
    const string secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
    string result = "";
    try
    {
    IJsonSerializer serializer = new JsonNetSerializer();
    IDateTimeProvider provider = new UtcDateTimeProvider();
    IJwtValidator validator = new JwtValidator(serializer, provider);
    IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
    IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
    IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);

    result = decoder.Decode(token, secret, verify: true);
    }
    catch (TokenExpiredException)
    {
    Console.WriteLine("Token has expired");
    }
    catch (SignatureVerificationException)
    {
    Console.WriteLine("Token has invalid signature");
    }
    catch (Exception EX)
    {
    Console.WriteLine("Token has invalid signature");
    }
    return result;
    }
    }
    }

  • 相关阅读:
    BZOJ 2152 聪聪可可
    hdu 1028 && hdu 1398 && hdu 1085 && hdu 1171 ——生成函数
    bzoj 4827 [Hnoi2017]礼物——FFT
    bzoj 4503 两个串——FFT
    bzoj 3527 [Zjoi2014]力——FFT
    bzoj 3160 万径人踪灭——FFT
    bzoj 2194 快速傅立叶之二
    bzoj 2179 FFT快速傅立叶
    洛谷 3803 【模板】多项式乘法(FFT)
    CF 622F The Sum of the k-th Powers——拉格朗日插值
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/13043844.html
Copyright © 2011-2022 走看看