示例#1
0
        public ClaimsPrincipal ReadShortToken(string token)
        {
            var tokenDecrypted = cryptService.Decrypt(CipherSecrets.ShortJwt, token);

            JwtSecurityToken jwtSecurityToken =
                new JwtSecurityToken(new JwtHeader(), JwtPayload.Deserialize(tokenDecrypted));

            ClaimsIdentity claimsIdentity = new ClaimsIdentity(jwtSecurityToken.Claims, SunJwt.Scheme);

            if (jwtSecurityToken.ValidTo.Add(TokensExpiration.Delta) < DateTime.UtcNow)
            {
                throw new Exception("Short token expires");
            }

            return(new ClaimsPrincipal(claimsIdentity));
        }
示例#2
0
        public virtual bool ValidateChangeEmailToken(string token, out int userId, out string email)
        {
            try
            {
                var tokenDecrypted = cryptService.Decrypt(CipherSecrets.EmailChange, token);

                var jwtSecurityToken = new JwtSecurityToken(new JwtHeader(), JwtPayload.Deserialize(tokenDecrypted));

                email  = jwtSecurityToken.Claims.First(x => x.Type == JwtRegisteredClaimNames.Email).Value;
                userId = int.Parse(jwtSecurityToken.Claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value);

                if (jwtSecurityToken.ValidTo.Add(TokensExpiration.Delta) < DateTime.UtcNow)
                {
                    return(false);
                }
            }
            catch
            {
                email  = null;
                userId = 0;
                return(false);
            }

            return(true);
        }