23 Mayıs 2022
Python PyJWT Equivelent in C#
using System.Security.Cryptography;
using System.Text;
public class PyJwt
{
public static string Encode(string clientId, string secret)
{
var algorithm = "{\"typ\":\"JWT\",\"alg\":\"HS256\"}";
var data = "{\"clientID\":\"" + clientId + "\"}";
var header = Convert.ToBase64String(Encoding.UTF8.GetBytes(algorithm));
var payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(data));
var key = Convert.ToBase64String(Encoding.UTF8.GetBytes(secret));
string signature = string.Empty;
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes($"{header}.{payload}"));
signature = Convert.ToBase64String(hash)
.Replace("+", "-")
.Replace("/", "_")
.Replace("=", "");
}
return $"{header}.{payload}.{signature}";
}
}