public bool IsTokenValid(string accessToken)
        {
            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrWhiteSpace(accessToken))
            {
                try
                {
                    var parser = new JsonWebToken <AccessTokenPayload> .Parser(accessToken);

                    var payload = parser.GetPayload();
                    var sign    = HashSignatureProvider.CreateHS256(secret);
                    var isValid = parser.Verify(sign);

                    var remainingExpDurationS = (payload.ExpirationDate - DateTime.UtcNow).TotalSeconds;

                    if (isValid && remainingExpDurationS > 0)
                    {
                        return(true);
                    }
                }

                catch
                {
                    return(false);
                }
            }

            return(false);
        }
        public string GenerateAccessToken(AccessTokenPayload tokenPayload)
        {
            var sign = HashSignatureProvider.CreateHS256(secret);
            var jwt  = new JsonWebToken <AccessTokenPayload>(tokenPayload, sign);
            var str  = jwt.ToEncodedString();

            return(str);
        }
Example #3
0
        /// <summary>
        /// Creates a hash signature.
        /// </summary>
        /// <returns>HashSignatureProvider created from non-null environment value of "JWT_SECRET", or literal "secret"</returns>
        private HashSignatureProvider Signature()
        {
            var secret = Environment.GetEnvironmentVariable("JWT_SECRET");

            return(HashSignatureProvider.CreateHS256(secret ?? "secret"));
        }