You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Jwt/JwtHelper.cs

43 lines
1.2 KiB

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<string, object> GetPayload(string token)
{
var secret = this._cfg["jwt:key"];
var payload = new JwtBuilder()
.WithSecret(secret)
.MustVerifySignature()
.Decode<IDictionary<string, object>>(token);
return payload;
}
public string GetToken(IDictionary<string, object> 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;
}
}
}