Aufgrund der Benutzung von Threads anstelle von Timern hat das Aufrufen der einzelnen Konversationen ca. 2 Sekunden gedauert. Die Synchronisationsdatei enthält, ob die letzte Nachrichte bereits gelesen wurde, sodass man, wenn man den Chat beendet bevor man eine neue Nachricht gelesen hat, diese nach dem erneuten Öffnen immer noch als neu angezeigt wird. - Dateinamen verbessert - Diverse Verbesserungen des Codes
458 lines
16 KiB
C#
458 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
using ChatController.HauptKlassen;
|
|
using ChatController.LoginKlassen;
|
|
using Newtonsoft.Json;
|
|
|
|
using Cursors = System.Windows.Input.Cursors;
|
|
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
|
using MessageBox = System.Windows.MessageBox;
|
|
using Path = System.IO.Path;
|
|
|
|
namespace ChatController
|
|
{
|
|
public partial class LoginControl
|
|
{
|
|
public delegate void LoginDelegate(ChatDatenUebergabe cdu);
|
|
|
|
public event LoginDelegate OnLogin;
|
|
|
|
private string _newUrl = "https://dict.ownchat.de/api/server/resolve/1/";
|
|
private string _benutzername = "";
|
|
private string _passwort = "";
|
|
private string _chatCode = "";
|
|
private string _kundennummer = "";
|
|
private string _authToken = "";
|
|
private string _version = "1.1";
|
|
|
|
public LoginControl()
|
|
{
|
|
InitializeComponent();
|
|
|
|
//encrypt verschlüsselte datei
|
|
LeseDateiFallsVorhanden();
|
|
|
|
//var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
lblVersion.Content = $"Version: {_version}";
|
|
|
|
#if DEBUG
|
|
TextBoxBenutzerName.Text = "MuellerL";
|
|
TextBoxPasswort.Password = "12345";
|
|
#endif
|
|
}
|
|
|
|
private void AnmeldenButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
Anmelden();
|
|
}
|
|
|
|
private void TextBoxPasswort_OnKeyUp(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Return)
|
|
{
|
|
Anmelden();
|
|
}
|
|
}
|
|
|
|
private void Anmelden()
|
|
{
|
|
try
|
|
{
|
|
//TODO Abfrage einführen, die prüft, ob eine Verbindung überhaupt hergestellt werden kann | Fehlermeldungen genauer definieren
|
|
if (!TextBoxKundennummer.Text.Equals("") && !TextBoxChatCode.Text.Equals("") && !TextBoxBenutzerName.Text.Equals("") && !TextBoxPasswort.Password.Equals(""))
|
|
{
|
|
Cursor = Cursors.Wait;
|
|
|
|
var _login = new Login(TextBoxKundennummer.Text, TextBoxChatCode.Text, TextBoxBenutzerName.Text, TextBoxPasswort.Password);
|
|
|
|
var x = _login.AnmeldevorgangDurchFuehren();
|
|
|
|
if (OnLogin != null && x != null)
|
|
{
|
|
AutosetDaten();
|
|
|
|
OnLogin?.Invoke(x);
|
|
}
|
|
|
|
Cursor = Cursors.Arrow;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Bitte alle Felder ausfüllen!", "Info", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
MessageBox.Show("Fehler: " + exception.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
Cursor = Cursors.Arrow;
|
|
}
|
|
}
|
|
|
|
#region Speichern / Laden / Löschen der Datei
|
|
|
|
#region wird vorerst nicht gebraucht
|
|
private void SpeichereDatenInDatei()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"))) {
|
|
using (StreamWriter sw = new StreamWriter(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt")))
|
|
{
|
|
|
|
Encoding enc = new UTF8Encoding();
|
|
//var byt = enc.GetBytes(TextBoxKundennummer.Text);
|
|
//var bytes = Convert.ToBase64String(byt);
|
|
|
|
//var byt2 = enc.GetBytes(TextBoxChatCode.Text);
|
|
//var bytes2 = Convert.ToBase64String(byt2);
|
|
|
|
var byt3 = enc.GetBytes(TextBoxBenutzerName.Text);
|
|
var bytes3 = Convert.ToBase64String(byt3);
|
|
|
|
|
|
var byt4 = enc.GetBytes(TextBoxPasswort.Password);
|
|
var bytes4 = Convert.ToBase64String(byt4);
|
|
|
|
//var byt5 = enc.GetBytes(Merken.IsChecked.ToString());
|
|
//var bytes5 = Convert.ToBase64String(byt5);
|
|
|
|
|
|
//sw.WriteLine(bytes);
|
|
//sw.WriteLine(bytes2);
|
|
sw.WriteLine(bytes3);
|
|
sw.WriteLine(bytes4);
|
|
//sw.WriteLine(bytes5);
|
|
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"), FileAttributes.ReadOnly);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Ups, da ist was schief gelaufen: \n" + e.Message, "Fehler", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
|
|
private void LoescheGespeicherteDatei()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt")))
|
|
{
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"), FileAttributes.Normal);
|
|
File.Delete(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"));
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Ups, da ist was schief gelaufen: \n" + e.Message, "Fehler", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private void LeseDateiFallsVorhanden()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChat.txt")))
|
|
{
|
|
List<string> merkdaten = new List<string>();
|
|
using (StreamReader sr = new StreamReader(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChat.txt"), true))
|
|
{
|
|
string line = "";
|
|
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
var encodedTextBytes = Convert.FromBase64String(line);
|
|
|
|
string plainText = Encoding.UTF8.GetString(encodedTextBytes);
|
|
|
|
merkdaten.Add(plainText);
|
|
}
|
|
}
|
|
|
|
if (merkdaten.Count == 2)
|
|
{
|
|
TextBoxKundennummer.Text = merkdaten[0];
|
|
TextBoxChatCode.Text = merkdaten[1];
|
|
}
|
|
}
|
|
|
|
|
|
//if (File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"))) {
|
|
|
|
// List<string> daten = new List<string>();
|
|
// using (StreamReader sr = new StreamReader(Path.Combine(GetAndCreateUserAppDataPath(), "OwnChatX1253781.txt"), true))
|
|
// {
|
|
// string line = "";
|
|
|
|
// while ((line = sr.ReadLine()) != null)
|
|
// {
|
|
// var encodedTextBytes = Convert.FromBase64String(line);
|
|
|
|
// string plainText = Encoding.UTF8.GetString(encodedTextBytes);
|
|
|
|
// daten.Add(plainText);
|
|
// }
|
|
// }
|
|
|
|
// if(daten.Count == 3) {
|
|
|
|
// TextBoxBenutzerName.Text = daten[0];
|
|
// TextBoxPasswort.Password = daten[1];
|
|
// // Merken.IsChecked = Convert.ToBoolean(daten[2]);
|
|
// }
|
|
//}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Ups, da ist was schief gelaufen: \n" + e.Message, "Fehler", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
|
|
private void AutosetDaten()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt")))
|
|
{
|
|
using (
|
|
StreamWriter sw = new StreamWriter(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt")))
|
|
{
|
|
|
|
Encoding enc = new UTF8Encoding();
|
|
var byt = enc.GetBytes(TextBoxKundennummer.Text);
|
|
var bytes = Convert.ToBase64String(byt);
|
|
|
|
var byt2 = enc.GetBytes(TextBoxChatCode.Text);
|
|
var bytes2 = Convert.ToBase64String(byt2);
|
|
|
|
sw.WriteLine(bytes);
|
|
sw.WriteLine(bytes2);
|
|
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt"),
|
|
FileAttributes.ReadOnly);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt"), FileAttributes.Normal);
|
|
File.Delete(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt"));
|
|
|
|
using (
|
|
StreamWriter sw = new StreamWriter(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt")))
|
|
{
|
|
|
|
Encoding enc = new UTF8Encoding();
|
|
var byt = enc.GetBytes(TextBoxKundennummer.Text);
|
|
var bytes = Convert.ToBase64String(byt);
|
|
|
|
var byt2 = enc.GetBytes(TextBoxChatCode.Text);
|
|
var bytes2 = Convert.ToBase64String(byt2);
|
|
|
|
sw.WriteLine(bytes);
|
|
sw.WriteLine(bytes2);
|
|
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), "ownChat.txt"),
|
|
FileAttributes.ReadOnly);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Ups, da ist was schief gelaufen: \n" + e.Message, "Fehler", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
|
|
private static string GetAndCreateUserAppDataPath()
|
|
{
|
|
try
|
|
{
|
|
string localAppData = Environment.GetFolderPath(
|
|
Environment.SpecialFolder.LocalApplicationData);
|
|
string companyFilePath
|
|
= Path.Combine(localAppData, "beyondSoft");
|
|
|
|
if (!Directory.Exists(companyFilePath))
|
|
Directory.CreateDirectory(companyFilePath);
|
|
|
|
string bewoFilePath
|
|
= Path.Combine(companyFilePath, "OwnChat");
|
|
|
|
if (!Directory.Exists(bewoFilePath))
|
|
Directory.CreateDirectory(bewoFilePath);
|
|
|
|
return bewoFilePath;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(
|
|
"Fehler beim Anlegen des Anwendungsordners. Sie besitzen nicht die erforderlichen Rechte, bitte wenden Sie sich an Ihren Systemadministrator.\n" +
|
|
ex.Message, "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
}
|
|
|
|
#endregion
|
|
|
|
//Neue Region zum VErschlüsseln und entschlüsseln der Daten
|
|
#region Verschlüsseln/Entschlüsseln via Link // wird ersmal nicht mehr gebraucht
|
|
|
|
//TODO Kann erst nach erfolgreichem Login verschlüsselt werden
|
|
|
|
private bool Verschluss()
|
|
{
|
|
try
|
|
{
|
|
string URl = _newUrl + "api/ownchat/user/encryptcredentials";
|
|
|
|
string postdaten = "";
|
|
|
|
Dictionary<string, string> postparameter = new Dictionary<string, string>();
|
|
|
|
postparameter.Add("username", _benutzername);
|
|
postparameter.Add("password", _passwort);
|
|
|
|
//neu | hinzufügen laut document
|
|
postparameter.Add("chatcode", _chatCode);
|
|
|
|
int i = 0;
|
|
foreach (var key in postparameter.Keys)
|
|
{
|
|
//hier noch chatcode übergeben
|
|
postdaten += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postparameter[key]) + "&";
|
|
|
|
}
|
|
|
|
WebRequest requesten = WebRequest.Create(URl);
|
|
|
|
requesten.Method = "POST";
|
|
requesten.ContentType = "application/x-www-form-urlencoded";
|
|
requesten.Headers["Token"] = _authToken;
|
|
requesten.Headers["CustomerID"] = _kundennummer;
|
|
|
|
byte[] daten = Encoding.ASCII.GetBytes(postdaten);
|
|
|
|
requesten.ContentLength = daten.Length;
|
|
|
|
try
|
|
{
|
|
Stream requestStream = requesten.GetRequestStream();
|
|
|
|
requestStream.Write(daten, 0, daten.Length);
|
|
|
|
requestStream.Close();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Es konnte keine Verbindung aufgebaut werden.", "Fehler", MessageBoxButton.OK);
|
|
return false;
|
|
}
|
|
|
|
//response bereich | Verarbeiten
|
|
HttpWebResponse myHttpWebResponse = (HttpWebResponse)requesten.GetResponse();
|
|
|
|
Stream responseStream = myHttpWebResponse.GetResponseStream();
|
|
|
|
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
|
|
|
|
var pageContent = myStreamReader.ReadToEnd();
|
|
|
|
|
|
//Bereich zum Verarbeiten der Erhaltenen Daten
|
|
var definiti = new { encryped = ""};
|
|
|
|
var jsonDatentyp = JsonConvert.DeserializeAnonymousType(pageContent, definiti);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Beim Verschlüsseln ist was schief gelaufen. \n" + e.Message,"Fehler",MessageBoxButton.OK);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private byte[] mediaDatei;
|
|
|
|
|
|
//TODO Entschluss weiter ausprogrammieren
|
|
private bool Entschluss()
|
|
{
|
|
try
|
|
{
|
|
string URl = _newUrl + "api/ownchat/user/decryptcredentials";
|
|
|
|
|
|
WebRequest requesten = WebRequest.Create(URl);
|
|
|
|
requesten.Method = "POST";
|
|
requesten.ContentType = "application/x-www-form-urlencoded";
|
|
requesten.Headers["CustomerID"] = _kundennummer;
|
|
|
|
byte[] daten = mediaDatei;
|
|
|
|
requesten.ContentLength = daten.Length;
|
|
|
|
try
|
|
{
|
|
Stream requestStream = requesten.GetRequestStream();
|
|
|
|
requestStream.Write(daten, 0, daten.Length);
|
|
|
|
requestStream.Close();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Es konnte keine Verbindung aufgebaut werden.", "Fehler", MessageBoxButton.OK);
|
|
return false;
|
|
}
|
|
|
|
//erhalte response
|
|
HttpWebResponse myHttpWebResponse = (HttpWebResponse)requesten.GetResponse();
|
|
|
|
Stream responseStream = myHttpWebResponse.GetResponseStream();
|
|
|
|
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
|
|
|
|
var pageContent = myStreamReader.ReadToEnd();
|
|
|
|
//Bereich zum Verarbeiten der Erhaltenen Daten
|
|
var definiti = new { encrypted = "" };
|
|
|
|
var jsonDatentyp = JsonConvert.DeserializeAnonymousType(pageContent, definiti);
|
|
|
|
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show("Beim Entschlüsseln ist was schief gelaufen. \n" + e.Message, "Fehler", MessageBoxButton.OK);
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
public void Login()
|
|
{
|
|
Anmelden();
|
|
}
|
|
}
|
|
}
|