private async void LoginCommand_Executed(object sender, ExecutedRoutedEventArgs e) { var connectionString = ConfigurationManager.ConnectionStrings["cs_login"].ConnectionString; var connection = new MySqlConnection(connectionString); var commandString = $"SELECT count(*) FROM laboratory_books.users WHERE user_name = '{TbxLogin.Text}';"; var sqlCommand = new MySqlCommand(commandString, connection); try { await connection.OpenAsync(); TbkConnectionStatus.Text = "trying to log in..."; //Check if user exist var userCheckResult = (long)(await sqlCommand.ExecuteScalarAsync()); if (userCheckResult > 0) { commandString = $"SELECT salt FROM laboratory_books.users WHERE user_name = '{TbxLogin.Text}'"; sqlCommand.CommandText = commandString; var salt = (string)(await sqlCommand.ExecuteScalarAsync()); var generatedHash = LoginHelper.GenerateHash(salt, TbxPassword.Password); commandString = $"SELECT count(*) FROM laboratory_books.users WHERE user_name = '{TbxLogin.Text}' AND password_hash ='{generatedHash}';"; sqlCommand.CommandText = commandString; var passwordAndUserCheckResult = (long)(await sqlCommand.ExecuteScalarAsync()); //Check if user and pasword match if (passwordAndUserCheckResult > 0) { TbkConnectionStatus.Text = "Successfully connected"; var foregraundBrush = new SolidColorBrush(Colors.Green); TbkConnectionStatus.Foreground = foregraundBrush; //create user var user = await LoginHelper.GetUserByNameAsync(TbxLogin.Text); this.BookUser = user; //get list of databases and fill combobox by their values var dbList = await LoginHelper.GetAvailableLaboratoryBooksAsync(TbxLogin.Text); CbxDataBases.DataContext = dbList; CbxDataBases.SelectedIndex = 0; CbxDataBases.IsEnabled = true; //Disable login and password controls TbxLogin.IsEnabled = false; TbxPassword.IsEnabled = false; IsLogged = true; } else { TbkConnectionStatus.Text = $"Wrong password"; var foregraundBrush = new SolidColorBrush(Colors.Red); TbkConnectionStatus.Foreground = foregraundBrush; } } else { var foregraundBrush = new SolidColorBrush(Colors.Red); TbkConnectionStatus.Text = "User does not exist"; TbkConnectionStatus.Foreground = foregraundBrush; } } finally { await connection.CloseAsync(); sqlCommand?.Dispose(); } }
private async void LoginCommandTwo_Executed(object sender, ExecutedRoutedEventArgs e) { //initialize report progress var progress = new Progress <string>(status => { TbkConnectionStatus.Text = status; }); var progressI = progress as IProgress <string>; var password = TbxPassword.Password; var userName = TbxLogin.Text; //initialize connection task var connectTask = new Task <User>(() => { var connectionString = ConfigurationManager.ConnectionStrings["cs_login"].ConnectionString; var connection = new MySqlConnection(connectionString); var commandString = $"SELECT count(*) FROM users WHERE user_name = '{userName}';"; var sqlCommand = new MySqlCommand(commandString, connection); connection.Open(); progressI.Report("trying to log in..."); //Check if user exist var userCheckResult = (long)(sqlCommand.ExecuteScalar()); if (userCheckResult == 0) { progressI.Report("User not found..."); } if (userCheckResult > 0) { progressI.Report("User found...checking password"); commandString = $"SELECT salt FROM users WHERE user_name = '{userName}'"; sqlCommand.CommandText = commandString; var salt = (string)sqlCommand.ExecuteScalar(); var generatedHash = LoginHelper.GenerateHash(salt, password); commandString = $"SELECT count(*) FROM users WHERE user_name = '{userName}' AND password_hash ='{generatedHash}';"; sqlCommand.CommandText = commandString; var passwordAndUserCheckResult = (long)sqlCommand.ExecuteScalar(); //Check if user and pasword match if (passwordAndUserCheckResult > 0) { progressI.Report("Password matched!"); //create user var user = LoginHelper.GetUserByName(userName); connection.Close(); sqlCommand?.Dispose(); return(user); //this.BookUser = user; } else { progressI.Report("Incorrect password!"); connection.Close(); sqlCommand?.Dispose(); } } return(null); }); try { connectTask.Start(); var user = await connectTask; if (user != null) { this.BookUser = user; this.IsLogged = true; } else { this.BookUser = null; this.IsLogged = false; } } catch (Exception exception) { MessageBox.Show ( exception.Message, "Loggin error", MessageBoxButton.OK, MessageBoxImage.Error ); } if (!this.IsLogged) { return; } // GetAvailableLaboratoryBooks var getAvailableBooksTask = new Task <ObservableCollection <string> >(() => { progressI.Report("Getting available books..."); var result = LoginHelper.GetAvailableLaboratoryBooks(userName); progressI.Report("Available books recieved..."); return(result); }); try { getAvailableBooksTask.Start(); var dbList = await getAvailableBooksTask; CbxDataBases.DataContext = dbList; CbxDataBases.SelectedIndex = 0; CbxDataBases.IsEnabled = true; TbkConnectionStatus.Text = "Succesfully logged!"; TbxLogin.IsEnabled = false; TbxPassword.IsEnabled = false; await Task.Run(() => LoginHelper.WriteLoginToFile(userName)); } catch (Exception exception) { MessageBox.Show ( exception.Message, "Getting books error", MessageBoxButton.OK, MessageBoxImage.Error ); } CbxDataBases.Focus(); }