Example #1
0
        /// <summary>
        /// Cria um token com claims específicos
        /// </summary>
        /// <param name="signingConfigurations"></param>
        /// <param name="tokenConfigurations"></param>
        /// <param name="username"></param>
        /// <param name="claims"></param>
        /// <returns></returns>
        public static JsonWebToken Generate(SigningConfigurations signingConfigurations, TokenConfigurations tokenConfigurations, string username, List <KeyValuePair <string, string> > claims)
        {
            ClaimsIdentity identity = CreateIdentity(username);

            foreach (var claim in claims)
            {
                identity.AddClaim(new Claim(claim.Key, claim.Value));
            }

            return(Generate(signingConfigurations, tokenConfigurations, identity));
        }
Example #2
0
        /// <summary>
        /// Cria um token recebendo um identity já criado
        /// </summary>
        /// <param name="signingConfigurations"></param>
        /// <param name="tokenConfigurations"></param>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static JsonWebToken Generate(SigningConfigurations signingConfigurations, TokenConfigurations tokenConfigurations, ClaimsIdentity identity)
        {
            DateTime dataCriacao   = DateTime.Now;
            DateTime dataExpiracao = dataCriacao + TimeSpan.FromDays(500);

            var handler       = new JwtSecurityTokenHandler();
            var securityToken = handler.CreateToken(new SecurityTokenDescriptor
            {
                Issuer             = tokenConfigurations.Issuer,
                Audience           = tokenConfigurations.Audience,
                SigningCredentials = signingConfigurations.SigningCredentials,
                Subject            = identity,
                NotBefore          = dataCriacao,
                Expires            = dataExpiracao,
            });
            var token = handler.WriteToken(securityToken);

            return(new JsonWebToken
            {
                Authenticated = true,
                Created = dataCriacao.ToString("yyyy-MM-dd HH:mm:ss"),
                Expiration = dataExpiracao.ToString("yyyy-MM-dd HH:mm:ss"),
                AccessToken = token,
                Message = "OK"
            });
        }
Example #3
0
 /// <summary>
 /// Cria um token com informações básicas do usuario
 /// </summary>
 /// <param name="signingConfigurations"></param>
 /// <param name="tokenConfigurations"></param>
 /// <param name="username"></param>
 /// <returns></returns>
 public static JsonWebToken Generate(SigningConfigurations signingConfigurations, TokenConfigurations tokenConfigurations, string username)
 => Generate(signingConfigurations, tokenConfigurations, CreateIdentity(username));