private UsuarioToken GeraToken(UsuarioRequisicao requisicao) { var claims = new[] { new Claim(JwtRegisteredClaimNames.UniqueName, requisicao.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var expiration = DateTime.UtcNow.AddHours(double.Parse(_configuration["TokenConfiguration:ExpireHours"])); JwtSecurityToken token = new JwtSecurityToken( issuer: _configuration["TokenConfiguration:Issuer"], audience: _configuration["TokenConfiguration:Audience"], claims: claims, expires: expiration, signingCredentials: credentials ); return(new UsuarioToken() { Authenticated = true, Token = new JwtSecurityTokenHandler().WriteToken(token), Expiration = expiration, Message = "Token gerado." }); }
public async Task <ActionResult> RegisterUser([FromBody] UsuarioRequisicao requisicao) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(e => e.Errors))); } var user = new IdentityUser { UserName = requisicao.Email, Email = requisicao.Email, EmailConfirmed = false }; var result = await _userManager.CreateAsync(user, requisicao.Password); if (!result.Succeeded) { return(BadRequest(result.Errors)); } await _signInManager.SignInAsync(user, false); return(Success(GeraToken(requisicao))); }
public async Task <ActionResult> Login([FromBody] UsuarioRequisicao requisicao) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(e => e.Errors))); } var result = await _signInManager.PasswordSignInAsync(requisicao.Email, requisicao.Password, isPersistent : false, lockoutOnFailure : false); if (result.Succeeded) { return(Success(GeraToken(requisicao))); } else { ModelState.AddModelError(string.Empty, "Login inválido...."); return(BadRequest(ModelState)); } }