using JWT.Algorithms; using JWT.Builder; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; namespace Infrastructure.Jwt { public class JwtHelper : IJwtHelper { private readonly IConfiguration _cfg; public JwtHelper(IConfiguration cfg) { this._cfg = cfg; } public IDictionary GetPayload(string token) { var secret = this._cfg["jwt:key"]; var payload = new JwtBuilder() .WithSecret(secret) .MustVerifySignature() .Decode>(token); return payload; } public string GetToken(IDictionary payload) { var secret = this._cfg["jwt:key"]; var builder = new JwtBuilder() .WithAlgorithm(new HMACSHA256Algorithm()) .WithSecret(secret) .AddClaim("exp", DateTimeOffset.UtcNow.AddYears(100).ToUnixTimeSeconds()); foreach (var item in payload) { builder.AddClaim(item.Key, item.Value); } var token = builder.Build(); return token; } } }