Example #1
0
        public JwtPayload ValidateToken(string token)
        {
            var appSetting = IoCHelper.GetInstance <IOptions <AppSettings> >();

            try
            {
                var json = new JwtBuilder()
                           .WithSecret(appSetting.Value.Secret)
                           .MustVerifySignature()
                           .Decode(token);

                var jwtJsonDecode = JsonConvert.DeserializeObject <JwtJsonDecode>(json);
                if (jwtJsonDecode == null || jwtJsonDecode.JwtPayload == null)
                {
                    return(null);
                }
                else
                {
                    return(jwtJsonDecode.JwtPayload);
                }
            }
            catch (TokenExpiredException)
            {
                return(null);
            }
            catch (SignatureVerificationException)
            {
                return(null);
            }
        }
Example #2
0
        public string GenerateToken(JwtPayload payload)
        {
            var appSetting = IoCHelper.GetInstance <IOptions <AppSettings> >();
            var token      = new JwtBuilder()
                             .WithAlgorithm(new HMACSHA256Algorithm())
                             .WithSecret(appSetting.Value.Secret)
                             .AddClaim("Expired", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
                             .AddClaim("JwtPayload", payload)
                             .Build();

            return(token);
        }