Ejemplo n.º 1
0
        public static string RefeshToken(this JwtSecurityToken jwt)
        {
            if (DictionaryToken.Any(n => n.Key.Equals(jwt.Subject, StringComparison.OrdinalIgnoreCase)) && jwt.ValidTo > DateTime.UtcNow)
            {
                jwt = new JwtSecurityToken(
                    issuer: JwtOptions().Value.Issuer,
                    audience: JwtOptions().Value.Audience,
                    claims: jwt.Claims,
                    notBefore: DateTime.UtcNow,
                    expires: DateTime.UtcNow.Add(JwtOptions().Value.Expiration),
                    signingCredentials: JwtOptions().Value.SigningCredentials);
                return(DictionaryToken[jwt.Subject] = new JwtSecurityTokenHandler().WriteToken(jwt));
            }

            //Token Expirated
            if (DictionaryToken.Any(n => n.Key.Equals(jwt.Subject, StringComparison.OrdinalIgnoreCase)))
            {
                DictionaryToken.Remove(DictionaryToken.FirstOrDefault(n => n.Key.Equals(jwt.Subject, StringComparison.OrdinalIgnoreCase)).Key);
            }
            return(null);
        }
Ejemplo n.º 2
0
 public static JwtSecurityToken CreateToken(this ClaimsIdentity Identity)
 {
     if (Identity != null && Identity.Claims.Count() > 0)
     {
         var _token = new JwtSecurityToken(
             issuer: JwtOptions().Value.Issuer,
             audience: JwtOptions().Value.Audience,
             claims: Identity.Claims,
             notBefore: DateTime.UtcNow,
             expires: DateTime.UtcNow.Add(JwtOptions().Value.Expiration),
             signingCredentials: JwtOptions().Value.SigningCredentials);
         if (DictionaryToken.Any(n => n.Key.Equals(_token.Subject, StringComparison.OrdinalIgnoreCase)))
         {
             DictionaryToken[_token.Subject] = new JwtSecurityTokenHandler().WriteToken(_token);
         }
         else
         {
             DictionaryToken.Add(_token.Subject, new JwtSecurityTokenHandler().WriteToken(_token));
         }
         return(_token);
     }
     return(null);
 }