예제 #1
0
        public IActionResult Login(LoginDTO login)
        {
            try
            {
                Usuarios usuario = _usuarioRepositorio.BuscarPorEmail(login);

                if (usuario == null)
                {
                    return(NotFound());
                }

                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Email, usuario.Email),
                    new Claim(JwtRegisteredClaimNames.Jti, usuario.Id.ToString()),
                    new Claim(ClaimTypes.Role, usuario.Permicao.ToString())
                };

                // Chave de acesso do token
                var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("SPmedgroup-chave-autenticacao"));


                //Credenciais do Token - Header
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);


                //Gera o token
                var token = new JwtSecurityToken(
                    issuer: "Senai_Semestre_Sprint2",
                    audience: "Senai_Semestre_Sprint2",
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(30),
                    signingCredentials: creds
                    );

                return(Ok(new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token)
                }));
            }
            catch (SystemException ex)
            {
                return(BadRequest());
            }
        }