Example #1
0
        private AuthenticationTokenDTO generateJwtToken(UsuarioDTO user, IEnumerable <PermisoDTO> permisos, IEnumerable <ControladorDTO> views, bool IsSuperUser)
        {
            // generate token that is valid for 7 days
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString())
                                                     , new Claim("user", user.Serialize())
                                                     , new Claim("permisos", permisos.Serialize()) }),
                Expires            = DateTime.UtcNow.AddDays(30),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(new AuthenticationTokenDTO
            {
                Token = tokenHandler.WriteToken(token),
                Usuario = user,
                IsSuperUser = IsSuperUser,
                Permisos = permisos,
                Views = views
            });
        }