public async Task <ActionResult <UserDTO> > Login(UserDTO userObj) { try { var user = await _userManager.FindByEmailAsync(userObj.Email); if (user is null) { return(NotFound()); } var result = await _signInManager .CheckPasswordSignInAsync(user, userObj.Password, false); if (result.Succeeded) { return(new UserDTO { Token = _jwtGenerator.CreateToken(user), UserName = user.UserName }); } return(Unauthorized()); } catch (Exception ex) { return(StatusCode(500, ex)); } }
public async Task <Response <AuthResponseDto> > Login(AuthRequestDto authRequestDto) { AuthRequestEntities authRequest = _mapper.Map <AuthRequestEntities>(authRequestDto); var response = new Response <AuthResponseDto>(); string todo = null; try { if (authRequestDto == null) { response.IsSuccess = false; response.IsWarning = true; response.CodigoError = "0"; response.Message = "Envio de parametros inválido,intente denuevo."; return(response); } else if (string.IsNullOrEmpty(authRequestDto.Username) || string.IsNullOrEmpty(authRequestDto.Password)) { response.IsSuccess = false; response.IsWarning = true; response.CodigoError = "0"; response.Message = "Envio de parametros inválido,intente denuevo."; return(response); } //valida el usuario y password var responseData = await _usuarioDomain.Login(authRequest); response.Data = _mapper.Map <AuthResponseDto>(responseData); var ver = Convert.ToInt32(todo); if (response.Data == null) { response.IsSuccess = false; response.IsWarning = true; response.CodigoError = "0"; response.Message = "Usuario y/o contraseña incorrecta."; return(response); } else { response.Data.Token = JwtGenerator.CreateToken(_configuration, response.Data.UserName); response.Message = "Inicio de sesión correctamente."; //guardar sesion de usuario var sesion = new SesionUsuarioDto { IdUser = response.Data.Id, Token = response.Data.Token, DateStart = DateTime.Now, Status = 1 }; var sesionUsuarioEntities = _mapper.Map <SesionUsuarioEntities>(sesion); var responseSesion = await _usuarioDomain.GuardarSesion(sesionUsuarioEntities); } } catch (Exception ex) { throw ex; } return(response); }