/// <summary> /// /// </summary> /// <param name="registerModel"></param> /// <returns></returns> public async Task <LoginApiViewModel> LoginApi(LoginApiGetViewModel logInModel) { LoginApiViewModel loginApiViewModel = new LoginApiViewModel(); var result = await SignInManager.PasswordSignInAsync(logInModel.Email, logInModel.Password, logInModel.RememberMe, shouldLockout : false); switch (result) { case SignInStatus.Success: var user = await UserManager.FindByNameAsync(logInModel.Email); if (user != null) { if (await UserManager.IsEmailConfirmedAsync(user.Id)) { LoginDetail(user); } loginApiViewModel.BirthDate = user.BirthDate; loginApiViewModel.Email = user.Email; loginApiViewModel.Gender = user.Gender; loginApiViewModel.LastName = user.Lastname; loginApiViewModel.Name = user.Name; loginApiViewModel.UserID = user.Id; loginApiViewModel.LoginErrorStatus = LoginErrorStatus.Success, } break; } return(loginApiViewModel); }
public IActionResult GetToken([FromBody] LoginApiViewModel user) { var usuario = new Usuario(); if (!usuario.Login(user.Usuario, user.Senha, ConexaoGeral, null)) { return(BadRequest("Usuário e/ou senha inválidos.")); } var symmetricKey = Convert.FromBase64String(Configuration["SecurityKey"]); var tokenHandler = new JwtSecurityTokenHandler(); var subject = new ClaimsIdentity(); subject.AddClaim(new Claim(ClaimTypes.Name, user.Usuario)); if (!string.IsNullOrEmpty(user.Cpf)) { using (var funcionario = new Funcionario()) { if (!funcionario.Login(user.Cpf, user.SenhaFuncionario, ConexaoGeral, null)) { return(BadRequest($"Usuário e/ou senha do funcionário inválidos!")); } subject.AddClaim(new Claim("IdFuncionario", funcionario.Id.ToString())); subject.AddClaim(new Claim(ClaimTypes.Role, funcionario.Nivel)); } } var tokenDescriptor = new SecurityTokenDescriptor { Subject = subject, //Audience = _configuration["Audience"], Issuer = Configuration["Issuer"], Expires = DateTime.UtcNow.AddMinutes(30), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature) }; var stoken = tokenHandler.CreateToken(tokenDescriptor); return(Ok(tokenHandler.WriteToken(stoken))); }