using System; using System.Collections.Generic; using System.Linq; using System.Web; using JWT; using JWT.Algorithms; using JWT.Serializers; using JWT.Builder; using JWT.Exceptions; namespace WebApplication1 { public static class JwtHelper { static string secret = System.Configuration.ConfigurationManager.AppSettings["secret"].ToString(); public static string CreateTocken(string UserName) { var tocken=JwtBuilder.Create().WithAlgorithm(new HMACSHA256Algorithm()) .ExpirationTime(DateTime.Now.AddMinutes(10)) .WithSecret(secret) .AddClaim("UserName",UserName) .AddClaim("DateTime",DateTime.Now) .WithValidationParameters( new ValidationParameters { ValidateExpirationTime = true, }).Encode(); ; return tocken; } public static bool Validate(string tocken) { try { var mes = JwtBuilder.Create() .WithAlgorithm(new HMACSHA256Algorithm()) .WithSecret(secret) .Decode(tocken); return true; } catch (TokenNotYetValidException ) { throw new Exception("Token is not valid yet"); } catch (TokenExpiredException) { throw new Exception("Token has expired"); } catch (SignatureVerificationException) { throw new Exception("Token has invalid signature"); } } } }