using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public static void Main()
var now = DateTime.UtcNow;
var secretClear = "NHB1WUxPNzRSTWJXdjNqbzJWanlnU1FDTVdjMDdseE5RM3MzbFdIb0RRUjY1VDFodmt4SjNzNkp6cmNqeGdqSjk5QXhiemFRdThsaVhEWTR3blNmUGs=";
var encodedSecret = GetSecretKeyAsBytes(SecretAsBase64Encoded(secretClear));
string token=Jose.JWT.Encode(GetPayload(now), encodedSecret, JwsAlgorithm.HS512);
Console.WriteLine("exp = " + GetExp(now));
Console.WriteLine("token = " + token);
private static string GetPayload(DateTime utcNow)
var json = "{\"Franquicia\":\"02681\",\"CodigoAbonado\":\"000000\",\"Nombre\":\"integracionpruebas\",\"TipoPlan\":\"000\",\"ReferenciaObligatoria\":false,\"DesgloseObligatorio\":false,\"TipologiaEnvio\":0,\"GestionRecogidasTTMM\":true,\"AsignarMensajeroTTMM\":true,\"ServiciosNacionales\":[],\"Direccion\":{\"CodigoPostal\":\"28522 \",\"Localidad\":\"RIVAS VACIAMADRID\",\"Provincia\":\"0260\",\"Direccion\":\"WRWRWER\",\"TipoVia\":\"EAV\",\"Numero\":11,\"Nombre\":\"RERERERWE\",\"Telefono\":\"3232132321\",\"Contacto\":\"WERERE\",\"Referencia\":\"REF123\",\"Observaciones\":\"OBSERV123\"},\"Departamentos\":[{\"Nombre\":\"0\",\"CodigoDepartamento\":\"0\"}]}";
private static byte[] GetSecretKeyAsBytes(string secret)
return Encoding.UTF8.GetBytes(secret);
public static string SecretAsBase64Encoded(string secret)
return Convert.ToBase64String(GetSecretKeyAsBytes(secret));
private static long GetNbf(DateTime utcNow) { return GetEpochDateTimeAsInt(utcNow); }
private static long GetExp(DateTime utcNow)
var tokenValidFor = TimeSpan.FromDays(200);
var expiry = utcNow.Add(tokenValidFor);
return GetEpochDateTimeAsInt(expiry);
public static long GetEpochDateTimeAsInt(DateTime datetime)
DateTime dateTime = datetime;
if (datetime.Kind != DateTimeKind.Utc)
dateTime = datetime.ToUniversalTime();
if (dateTime.ToUniversalTime() <= UnixEpoch)
return (long)(dateTime - UnixEpoch).TotalSeconds;