C#JWT验证

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using JWT;
using Newtonsoft.Json;

namespace ConsoleApplication39
{
class Program
{
static void Main(string[] args)
{
UU u = new UU() { iae = "AA", Inat = "BB", mme = "CC" };
string token = new AA().GetToken(u);
UU w = new AA().GetUU(token);
Console.Read();
}
}
class AA
{
//密钥
private string key = "glawefof";
public string GetToken(UU u)
{
JWT.Algorithms.IJwtAlgorithm Algorithm = new JWT.Algorithms.HMACSHA256Algorithm();
JWT.IJsonSerializer json = new JS();
JWT.IBase64UrlEncoder Base64 = new JWT.JwtBase64UrlEncoder();

JwtEncoder en = new JwtEncoder(Algorithm, json, Base64);
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
return en.Encode(u, keyBytes);
}

public UU GetUU(string token)
{
IJsonSerializer js = new JS();
IJwtValidator validator = new JwtValidator(js, new JWT.UtcDateTimeProvider());
JWT.IBase64UrlEncoder Base64 = new JWT.JwtBase64UrlEncoder();
JwtDecoder en = new JwtDecoder(js,validator, Base64);
return en.DecodeToObject<UU>(token);
}
}
class UU
{
public string mme { get; set; }
public string Inat { get; set; }
public string iae { get; set; }
}

class JS : JWT.IJsonSerializer
{
public T Deserialize<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}

public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}
}
}

猜你喜欢

转载自www.cnblogs.com/luchaobooks/p/10953927.html