private void login(object sender, RoutedEventArgs e) { // Setup custom message box CustomMessageBox customMessageBox = new CustomMessageBox(kinectSensorChooser); // Check if username entered is at least 3 characters long if (textBoxUsername.Text.Length < 3) { // Show error message dialog customMessageBox.ShowText("Username is at least 3 characters long!"); } // Check password length is at least 3 characters long else if (passwordBox.Password.Length < 3) { // Show error message dialog customMessageBox.ShowText("Password is at least 3 characters long!"); } else { // Create the player repository object PlayersRepository pro = new PlayersRepository(); // Hash the password SHA256 sha256 = SHA256.Create(); byte[] bytes = Encoding.UTF8.GetBytes(passwordBox.Password); byte[] hash = sha256.ComputeHash(bytes); StringBuilder result = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { result.Append(hash[i].ToString("X2")); } // Search database for this username List <PlayersRepository.PlayerDto> player = pro.GetPlayerWithUsername(textBoxUsername.Text); // If player exist if (player.Count == 1) { // If password match then log them in if (result.ToString().Contains(player[0].Password)) { // Retrieve and save the player ID playerID = player[0].Id; // Show dialog that the login is successful customMessageBox.ShowText("Successfully logged in"); // Close after succesfully logged in Close(); } else { // Show dialog that the login is not successful customMessageBox.ShowText("Password is incorrect!"); // Clear the incorrect password passwordBox.Password = ""; } } else { // Show dialog that the login is not successful customMessageBox.ShowText("Player doesn't exist!"); } } }
// Execute press functions private void HandPointerPressReleaseEvent(object sender, HandPointerEventArgs e) { if (capturedHandPointer == e.HandPointer) { if (e.HandPointer.GetIsOver(btnCancelLogin)) { Close(); VisualStateManager.GoToState(btnCancelLogin, "MouseOver", true); } else if (e.HandPointer.GetIsOver(btnLogin)) { // Setup custom message box CustomMessageBox customMessageBox = new CustomMessageBox(kinectSensorChooser); // Check if username entered is at least 3 characters long if (textBoxUsername.Text.Length < 3) { // Show error message dialog customMessageBox.ShowText("Username is at least 3 characters long!"); } // Check password length is at least 3 characters long else if (passwordBox.Password.Length < 3) { // Show error message dialog customMessageBox.ShowText("Password is at least 3 characters long!"); } else { // Create the player repository object PlayersRepository pro = new PlayersRepository(); // Hash the password SHA256 sha256 = SHA256.Create(); byte[] bytes = Encoding.UTF8.GetBytes(passwordBox.Password); byte[] hash = sha256.ComputeHash(bytes); StringBuilder result = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { result.Append(hash[i].ToString("X2")); } // Search database for this username List <PlayersRepository.PlayerDto> player = pro.GetPlayerWithUsername(textBoxUsername.Text); // If player exist if (player.Count == 1) { // If password match then log them in if (result.ToString().Contains(player[0].Password)) { // Retrieve and save the player ID playerID = player[0].Id; // Show dialog that the login is successful customMessageBox.ShowText("Successfully logged in"); // Close after succesfully logged in Close(); } else { // Show dialog that the login is not successful customMessageBox.ShowText("Password is incorrect!"); // Clear the incorrect password passwordBox.Password = ""; } } else { // Show dialog that the login is not successful customMessageBox.ShowText("Player doesn't exist!"); } } VisualStateManager.GoToState(btnLogin, "MouseOver", true); } else if (e.HandPointer.GetIsOver(textBoxUsername)) { textBoxUsername.Focus(); VisualStateManager.GoToState(textBoxUsername, "MouseOver", true); } else if (e.HandPointer.GetIsOver(passwordBox)) { passwordBox.Focus(); VisualStateManager.GoToState(passwordBox, "MouseOver", true); } else { try { if (e.HandPointer.GetIsOver((Button)sender)) { Button button = null; try { button = (Button)sender; } catch (Exception) { } if (button.Equals(btnA)) { keyPressed = 'a'; } else if (button.Equals(btnB)) { keyPressed = 'b'; } else if (button.Equals(btnC)) { keyPressed = 'c'; } else if (button.Equals(btnD)) { keyPressed = 'd'; } else if (button.Equals(btnE)) { keyPressed = 'e'; } else if (button.Equals(btnF)) { keyPressed = 'f'; } else if (button.Equals(btnG)) { keyPressed = 'g'; } else if (button.Equals(btnH)) { keyPressed = 'h'; } else if (button.Equals(btnI)) { keyPressed = 'i'; } else if (button.Equals(btnJ)) { keyPressed = 'j'; } else if (button.Equals(btnK)) { keyPressed = 'k'; } else if (button.Equals(btnL)) { keyPressed = 'l'; } else if (button.Equals(btnM)) { keyPressed = 'm'; } else if (button.Equals(btnN)) { keyPressed = 'n'; } else if (button.Equals(btnO)) { keyPressed = 'o'; } else if (button.Equals(btnP)) { keyPressed = 'p'; } else if (button.Equals(btnQ)) { keyPressed = 'q'; } else if (button.Equals(btnR)) { keyPressed = 'r'; } else if (button.Equals(btnS)) { keyPressed = 's'; } else if (button.Equals(btnT)) { keyPressed = 't'; } else if (button.Equals(btnU)) { keyPressed = 'u'; } else if (button.Equals(btnV)) { keyPressed = 'v'; } else if (button.Equals(btnW)) { keyPressed = 'w'; } else if (button.Equals(btnX)) { keyPressed = 'x'; } else if (button.Equals(btnY)) { keyPressed = 'y'; } else if (button.Equals(btnZ)) { keyPressed = 'z'; } else if (button.Equals(btnBackspace)) { keyPressed = '\b'; } if (textBoxUsername.IsFocused) { int usernameCaretIndex = textBoxUsername.CaretIndex; if (keyPressed == '\b' && textBoxUsername.Text.Length > 0) { textBoxUsername.Text = textBoxUsername.Text.Remove(usernameCaretIndex - 1, 1); textBoxUsername.CaretIndex = usernameCaretIndex - 1; } else { textBoxUsername.Text = textBoxUsername.Text.Insert(usernameCaretIndex, keyPressed.ToString()); textBoxUsername.CaretIndex = usernameCaretIndex + 1; } } else if (passwordBox.IsFocused) { // Due to security reason, caret index of passwordbox is not retrievable if (keyPressed == '\b' && passwordBox.Password.Length > 0) { // Remove all password passwordBox.Password = ""; } else { // Use TextCompositionManager to input at current caret TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, passwordBox, keyPressed.ToString())); } } } else { VisualStateManager.GoToState(btnCancelLogin, "Normal", true); VisualStateManager.GoToState(btnLogin, "Normal", true); VisualStateManager.GoToState(textBoxUsername, "Normal", true); VisualStateManager.GoToState(passwordBox, "Normal", true); VisualStateManager.GoToState(btnA, "Normal", true); VisualStateManager.GoToState(btnB, "Normal", true); VisualStateManager.GoToState(btnC, "Normal", true); VisualStateManager.GoToState(btnD, "Normal", true); VisualStateManager.GoToState(btnE, "Normal", true); VisualStateManager.GoToState(btnF, "Normal", true); VisualStateManager.GoToState(btnH, "Normal", true); VisualStateManager.GoToState(btnI, "Normal", true); VisualStateManager.GoToState(btnJ, "Normal", true); VisualStateManager.GoToState(btnK, "Normal", true); VisualStateManager.GoToState(btnL, "Normal", true); VisualStateManager.GoToState(btnM, "Normal", true); VisualStateManager.GoToState(btnN, "Normal", true); VisualStateManager.GoToState(btnO, "Normal", true); VisualStateManager.GoToState(btnP, "Normal", true); VisualStateManager.GoToState(btnQ, "Normal", true); VisualStateManager.GoToState(btnR, "Normal", true); VisualStateManager.GoToState(btnS, "Normal", true); VisualStateManager.GoToState(btnT, "Normal", true); VisualStateManager.GoToState(btnU, "Normal", true); VisualStateManager.GoToState(btnV, "Normal", true); VisualStateManager.GoToState(btnW, "Normal", true); VisualStateManager.GoToState(btnX, "Normal", true); VisualStateManager.GoToState(btnY, "Normal", true); VisualStateManager.GoToState(btnZ, "Normal", true); VisualStateManager.GoToState(btnBackspace, "Normal", true); } } catch (Exception) { } } e.HandPointer.Capture(null); e.Handled = true; } }