Programm_Wirl/AA-Forms/FormLogin.cs
2025-11-30 11:51:28 +01:00

180 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using DatenDB;
using System.Threading;
using System.Diagnostics;
namespace Deckungsbeitrag
{
public partial class FormLogin : Form
{
string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
string keyboardPath;
public FormLogin()
{
InitializeComponent();
}
private void buttonAnmelden_Click(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(this.textBoxPasswort.Text)) textBoxBenutzer_Leave(sender, e);
try
{
MD5 md5 = MD5.Create();
byte[] md5hash = md5.ComputeHash(Encoding.UTF8.GetBytes(this.textBoxPasswort.Text));
string pwdHash = string.Empty;
foreach (byte b in md5hash) pwdHash += b.ToString("x2");
this.Person = Benutzer.Get(this.textBoxBenutzer.Text, pwdHash);
this.Person.Save();
this.DialogResult = DialogResult.OK;
this.Close();
}
catch (LoginException lex)
{
switch (lex.ErrorCode)
{
case -1:
MessageBox.Show(lex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.buttonRegistrieren.Visible = false;
break;
case -2:
MessageBox.Show(lex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.buttonRegistrieren.Visible = false;
break;
case -3:
MessageBox.Show(lex.Message, "Registrieren", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.buttonRegistrieren.Visible = true;
this.buttonAnmelden.Enabled = false;
this.groupBox1.Height = 146;
this.Height = 265;
this.labelPwdWh.Visible = true;
this.textBoxPwdWh.Visible = true;
break;
case -4:
MessageBox.Show(lex.Message, "Server Fehler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
CloseKeyboard();
}
private void CloseKeyboard()
{
foreach (var process in Process.GetProcessesByName("TabTip"))
{
try
{
process.Close();
}
catch (Exception es)
{
// Fehlerbehandlung falls Prozess nicht beendet werden kann
MessageBox.Show($"Fehler beim Schließen: " + es.Message);
}
}
}
private void buttonAbbrechen_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
public Benutzer Person { get; set; }
private void buttonRegistrieren_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.textBoxBenutzer.Text) && !String.IsNullOrEmpty(this.textBoxPasswort.Text) && this.textBoxPasswort.Text == this.textBoxPwdWh.Text)
{
Benutzer person = Benutzer.GetBenutzer(this.textBoxBenutzer.Text, null);
//person.BenutzerName = this.textBoxBenutzer.Text;
MD5 md5 = MD5.Create();
byte[] md5hash = md5.ComputeHash(Encoding.UTF8.GetBytes(this.textBoxPasswort.Text));
string pwdHash = string.Empty;
foreach (byte b in md5hash) pwdHash += b.ToString("x2");
person.Passwort = pwdHash;
if (person.Save() == 1)
{
this.groupBox1.Height = 100;
this.Height = 222;
this.labelPwdWh.Visible = false;
this.textBoxPwdWh.Visible = false;
this.buttonRegistrieren.Visible = false;
this.textBoxPasswort.Text = this.textBoxPwdWh.Text = string.Empty;
MessageBox.Show($"Benutzer {this.textBoxBenutzer.Text} wurde erfolgreich angelegt!", "Hinweis", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.buttonAnmelden.Enabled = true;
}
}
}
private void textBoxBenutzer_Leave(object sender, EventArgs e)
{
if (textBoxBenutzer.Text.Contains(";"))
{
this.textBoxPasswort.Text = this.textBoxBenutzer.Text.Substring(this.textBoxBenutzer.Text.IndexOf(";") + 1);
this.textBoxBenutzer.Text = this.textBoxBenutzer.Text.Substring(0, this.textBoxBenutzer.Text.IndexOf(";"));
}
}
private void FormLogin_Load(object sender, EventArgs e)
{
this.groupBox1.Height = 110;
this.labelPwdWh.Visible = false;
this.textBoxPwdWh.Visible = false;
this.buttonRegistrieren.Visible = false;
this.textBoxBenutzer.Focus();
WindowState = FormWindowState.Normal;
StartPosition = FormStartPosition.CenterScreen;
}
private void FormLogin_Shown(object sender, EventArgs e)
{
StartPosition = FormStartPosition.CenterScreen;
}
private void textBoxBenutzer_Enter(object sender, EventArgs e)
{
// Pfad zur Touch-Tastatur
keyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
// Bildschirmtastatur starten
Process.Start(keyboardPath);
}
private void textBoxPasswort_Enter(object sender, EventArgs e)
{
// Pfad zur Touch-Tastatur
keyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
// Bildschirmtastatur starten
Process.Start(keyboardPath);
}
private void textBox_Leave(object sender, EventArgs e)
{
CloseKeyboard();
}
}
}