Пример #1
0
        public IActionResult Logar()
        {
            var usuario = new UsuarioViewModelOutPut
            {
                Codigo = 1,
                Login  = "******",
                Email  = "*****@*****.**"
            };

            var token = _authenticationService.GerarToken(usuario);

            return(Ok(new
            {
                Token = token,
                Usuario = usuario
            }));
        }
Пример #2
0
        public string GerarToken(UsuarioViewModelOutPut usuarioViewModelOut)
        {
            var secret = Encoding.ASCII.GetBytes(_configuration.GetSection("JwTConfigurations:Secret").Value);
            var symmetricSecurityKey    = new SymmetricSecurityKey(secret);
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, usuarioViewModelOut.Codigo.ToString()),
                    new Claim(ClaimTypes.Name, usuarioViewModelOut.Login),
                    new Claim(ClaimTypes.Email, usuarioViewModelOut.Email)
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature)
            };

            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var tokenGenerate           = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
            var token = jwtSecurityTokenHandler.WriteToken(tokenGenerate);

            return(token);
        }