public async Task <IActionResult> Logout([FromBody] AuthenticationModelRequest Request) { var response = new AuthenticationModelResponse(); response.UrlApi = HttpContext.Request.Path.Value; try { if (Request.Logout == null) { response.ErrorCode = -1; response.ErrorMessage = "Request! inválido."; response.Success = false; return(BadRequest(response)); } var RemoteIpAddress = HttpContext.Connection.RemoteIpAddress; string IPAddress = RemoteIpAddress.MapToIPv4().ToString().Trim(); string Hostname = Dns.GetHostEntry(RemoteIpAddress).HostName.Trim(); var logout = await _userService.Logout(new sp_logout() { Code = Request.Logout.Code, IPAddress = IPAddress, Hostname = Hostname }); response.Logout = _mapper.Map <LogoutModelResponse>(logout); response.Success = true; return(Ok(response)); } catch (Exception ex) { response.ErrorMessage = $"Error: {ex.Message}"; response.Success = false; return(StatusCode(500, response)); } }
public async Task <IActionResult> Login([FromBody] AuthenticationModelRequest Request) { var response = new AuthenticationModelResponse(); response.UrlApi = HttpContext.Request.Path.Value; try { if (Request.Login == null) { response.ErrorCode = -1; response.ErrorMessage = "Request! inválido."; response.Success = false; return(BadRequest(response)); } var RemoteIpAddress = HttpContext.Connection.RemoteIpAddress; string IPAddress = RemoteIpAddress.MapToIPv4().ToString().Trim(); string Hostname = Dns.GetHostEntry(RemoteIpAddress).HostName.Trim(); // Verify User User User = await _userService.Login(new sp_login() { Username = Request.Login.Username, Password = Request.Login.Password, IPAddress = IPAddress, Hostname = Hostname }); if (User.ID <= 0) { response.ErrorCode = -1; response.ErrorMessage = "Usuario no identificado."; response.Success = false; return(NotFound(response)); } // Date var Now = DateTime.Now; var ExpiresDate = Now.AddMinutes(int.Parse(_configuration["JWT:Expires"])); // Create Token string Token = this.GetToken(new User() { Username = Request.Login.Username }, Now, ExpiresDate); if (Token == null) { response.ErrorCode = -1; response.ErrorMessage = "El Token de seguridad no fue creado correctamente."; response.Success = false; return(StatusCode(500, response)); } // Add Session UserSession userSession = await _userSessionService.Add(new UserSession() { UserID = User.ID, Token = Token, NotBeforeDate = Now, ExpiresDate = ExpiresDate, LoginDate = Now, LogoutDate = null, StateID = 1, CreatedBy = User.ID, CreateDate = Now, HostnameCreated = Hostname, IPAddressCreated = IPAddress, UpdatedBy = User.ID, UpdateDate = Now, HostnameUpdated = Hostname, IPAddressUpdated = IPAddress }); IEnumerable <Business.Core.System> systems = await _systemService.GetByUser(User.ID); // Response response.Login = _mapper.Map <LoginModelResponse>(userSession); response.User = _mapper.Map <UserModelResponse>(User); response.Systems = _mapper.Map <ICollection <SystemModelResponse> >(systems); response.Success = true; return(Ok(response)); } catch (Exception ex) { response.ErrorMessage = $"Error: {ex.Message}"; response.Success = false; return(StatusCode(500, response)); } }