# Conflicts: # PrototypWPFHAG/MainWindow.xaml # PrototypWPFHAG/MainWindow.xaml.cs
		
			
				
	
	
		
			140 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			140 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System.Data;
 | |
| using System.Runtime.InteropServices;
 | |
| using System.Security.Principal;
 | |
| using System.Text;
 | |
| using System.Windows;
 | |
| using System.Windows.Controls;
 | |
| using System.Windows.Data;
 | |
| using System.Windows.Documents;
 | |
| using System.Windows.Input;
 | |
| using System.Windows.Media;
 | |
| using System.Windows.Media.Imaging;
 | |
| using System.Windows.Navigation;
 | |
| using System.Windows.Shapes;
 | |
| using MahApps.Metro.Controls;
 | |
| using Npgsql;
 | |
| using Npgsql.Replication;
 | |
| 
 | |
| namespace PrototypWPFHAG;
 | |
| 
 | |
| /// <summary>
 | |
| /// Interaction logic for MainWindow.xaml
 | |
| /// </summary>
 | |
| public partial class MainWindow : MetroWindow
 | |
| {
 | |
|     public MainWindow()
 | |
|     {
 | |
|         InitializeComponent();
 | |
| <<<<<<< HEAD
 | |
|         //TestConnection();
 | |
|         //Console.ReadKey();
 | |
| 
 | |
|     }
 | |
| 
 | |
|     private static void TestConnection()
 | |
| =======
 | |
|     }
 | |
| 
 | |
|     private void Window_Loaded(object sender, RoutedEventArgs e)
 | |
|     {
 | |
|         UsernameTextBox.Focus(); // Focus first TextBox on start
 | |
|     }
 | |
| 
 | |
|     private static NpgsqlConnection GetConnection()
 | |
|     {
 | |
|         return new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=postgres;");
 | |
|     }
 | |
| 
 | |
|     private string ValidateUser(string username, string password)
 | |
| >>>>>>> 1195250b98172d34b8c49b85a81c21d1f26cdbac
 | |
|     {
 | |
|         using (NpgsqlConnection con = GetConnection())
 | |
|         {
 | |
|             con.Open();
 | |
| 
 | |
|             // Check for correct UserName
 | |
|             string userQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username";
 | |
|             using (NpgsqlCommand userCmd = new NpgsqlCommand(userQuery, con))
 | |
|             {
 | |
|                 userCmd.Parameters.AddWithValue("@username", username);
 | |
|                 int userExists = Convert.ToInt32(userCmd.ExecuteScalar());
 | |
| 
 | |
|                 if (userExists == 0)
 | |
|                 {
 | |
|                     return "Dieser Benutzername existiert nicht";
 | |
|                 }
 | |
|             }
 | |
|             
 | |
|             // Check for correct Password
 | |
|             string pwQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username AND \"Password\" = @password";
 | |
|             using (NpgsqlCommand pwCmd = new NpgsqlCommand(pwQuery, con))
 | |
|             {
 | |
|                 pwCmd.Parameters.AddWithValue("@username", username);
 | |
|                 pwCmd.Parameters.AddWithValue("@password", password);
 | |
|                 int correctPassword = Convert.ToInt32(pwCmd.ExecuteScalar());
 | |
| 
 | |
|                 if (correctPassword == 0)
 | |
|                 {
 | |
|                     return "Falsches Password!";
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| <<<<<<< HEAD
 | |
|     }
 | |
|     private static NpgsqlConnection GetConnection()
 | |
|     {
 | |
|         return new NpgsqlConnection(@"Server=localhost;Port=7854;User Id=postgres;Database=postgres;");
 | |
| =======
 | |
| 
 | |
|         return "Erfolgreich";
 | |
| >>>>>>> 1195250b98172d34b8c49b85a81c21d1f26cdbac
 | |
|     }
 | |
| 
 | |
|     private void ShowPasswordButton_PreviewMouseDown(object sender, RoutedEventArgs e)
 | |
|     {
 | |
|         PasswordVisibleTextBox.Text = PasswordTextBox.Password; // Write password in the visible box
 | |
|         PasswordVisibleTextBox.Visibility = Visibility.Visible;
 | |
|         PasswordTextBox.Visibility = Visibility.Collapsed;
 | |
|     }
 | |
| 
 | |
|     private void ShowPasswordButton_Click(object sender, RoutedEventArgs e)
 | |
|     {
 | |
|         if (PasswordTextBox.Visibility == Visibility.Visible)
 | |
|         {
 | |
|             // Show password
 | |
|             PasswordVisibleTextBox.Text = PasswordTextBox.Password;
 | |
|             PasswordVisibleTextBox.Visibility = Visibility.Visible;
 | |
|             PasswordTextBox.Visibility = Visibility.Collapsed;
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             // Hide password
 | |
|             PasswordTextBox.Visibility = Visibility.Visible;
 | |
|             PasswordVisibleTextBox.Visibility = Visibility.Collapsed;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // Syncs password with visible TextBox
 | |
|     private void PasswordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
 | |
|     {
 | |
|         PasswordVisibleTextBox.Text = PasswordTextBox.Password;
 | |
|     }
 | |
| 
 | |
|     private void loginButton_Click(object sender, RoutedEventArgs e)
 | |
|     {
 | |
|         string username = UsernameTextBox.Text;
 | |
|         string password = PasswordTextBox.Password;
 | |
|         string result = ValidateUser(username, password);
 | |
| 
 | |
|         if (result == "Erfolgreich")
 | |
|         {
 | |
|             SearchWindow searchWindow = new();
 | |
|             searchWindow.Show(); // Open new window
 | |
|             this.Close(); // Close this window
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             MessageBox.Show(result, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
 | |
|         }
 | |
|     }
 | |
| } |