367 lines
13 KiB
C#
367 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Windows;
|
|
using ChatController.LoginKlassen;
|
|
using ChatController.Utilities;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
namespace ChatController.HauptKlassen
|
|
{
|
|
public class Login
|
|
{
|
|
private string _Url = "";
|
|
|
|
private readonly string _NewUrl = "https://dict.ownchat.de/api/server/resolve/1/";
|
|
|
|
public string ServerUrl { get; set; } = "";
|
|
private string _AuthToken = "";
|
|
|
|
private readonly string _UserName;
|
|
private readonly string _Password;
|
|
private readonly string _Tenant;
|
|
private readonly string _ChatCode;
|
|
private readonly string _ApiKey;
|
|
|
|
private long? _UserId;
|
|
|
|
private bool _ownchatVerbindungsaufbauOK;
|
|
private bool _gruppeholenverbindungsaufbauOK;
|
|
|
|
private AuthenticationData _serverconect;
|
|
private UserDaten _LoggedInUser;
|
|
private ChatGruppenDaten _AllGroups;
|
|
|
|
private readonly bool _ShouldShowMessageBox = true;
|
|
|
|
public Login(string pTenant, string pChatCode, string pUserName, string pPassword)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ChatCode = pChatCode;
|
|
_UserName = pUserName;
|
|
_Password = pPassword;
|
|
_ApiKey = "0000";//default, damit nichts kaputt geht
|
|
}
|
|
|
|
public Login(string pTenant, string pChatCode, string pUserName, string pPassword, string pApiKey)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ChatCode = pChatCode;
|
|
_UserName = pUserName;
|
|
_Password = pPassword;
|
|
_ApiKey = pApiKey;
|
|
}
|
|
|
|
//
|
|
public Login(string pTenant)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ShouldShowMessageBox = false;
|
|
ServerErmittlung();
|
|
}
|
|
|
|
public ChatDatenUebergabe AnmeldevorgangDurchFuehren()
|
|
{
|
|
if(ServerErmittlung()){
|
|
var dic = new Dictionary<string, string>
|
|
{
|
|
{"username", _UserName},
|
|
{ "password", _Password},
|
|
{ "chatcode", _ChatCode}
|
|
};
|
|
|
|
VerbindeMitChatServer(dic);
|
|
|
|
if (_ownchatVerbindungsaufbauOK)
|
|
{
|
|
GetGroups();
|
|
}
|
|
|
|
|
|
if (_gruppeholenverbindungsaufbauOK)
|
|
{
|
|
|
|
var maxUploadSize = GetMaiximumAllowedFileUploadSize(_AuthToken,_Tenant);
|
|
|
|
var ubergabe = new ChatDatenUebergabe(_serverconect, _LoggedInUser, _AllGroups, ServerUrl, _AuthToken, _UserId, _Tenant,_ApiKey, maxUploadSize);
|
|
|
|
return ubergabe;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public bool ServerErmittlung()
|
|
{
|
|
if (string.IsNullOrEmpty(ServerUrl))
|
|
{
|
|
try
|
|
{
|
|
var serverURl = _NewUrl + _Tenant;
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
var request = WebRequest.Create(serverURl);
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
|
|
WebResponse response;
|
|
|
|
try
|
|
{
|
|
response = request.GetResponse();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Es konnte keine Verbindung aufgebaut werden.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
var responseFromServer = Utils.ReadStream(response);
|
|
|
|
try
|
|
{
|
|
var jsonDatentyp = JsonConvert.DeserializeObject<AuthenticationDataType1>(responseFromServer);
|
|
|
|
switch(jsonDatentyp.Status)
|
|
{
|
|
case 1:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Kundennummer unbekannt.\nBitte überprüfen Sie die Anmeldeinformationen.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
case 2:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Der Diensttyp ist für die angegebene Kundennummer nicht definiert.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
_Url = jsonDatentyp.Url;
|
|
}
|
|
catch (Exception eds)
|
|
{
|
|
try
|
|
{
|
|
var jsonDatentyp = JsonConvert.DeserializeObject<AuthenticationDataType2>(responseFromServer);
|
|
|
|
switch(jsonDatentyp.Status)
|
|
{
|
|
case 1:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Kundennummer unbekannt.\nBitte überprüfen Sie die Anmeldeinformationen.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
case 2:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Der Diensttyp ist für die angegebene Kundennummer nicht definiert.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
_Url = jsonDatentyp.Url;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "ownChat Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
response.Close();
|
|
|
|
ServerUrl = _Url;
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "ownChat Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void VerbindeMitChatServer(Dictionary<string, string> postparameter)
|
|
{
|
|
try
|
|
{
|
|
var endurl = _Url + "/api/user/loginwithchatcode";
|
|
|
|
var postdaten = "";
|
|
|
|
var i = 0;
|
|
foreach (var key in postparameter.Keys)
|
|
{
|
|
postdaten += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postparameter[key]) + "&";
|
|
}
|
|
|
|
var webRequest = WebRequest.Create(endurl);
|
|
|
|
webRequest.Method = "POST";
|
|
webRequest.ContentType = "application/x-www-form-urlencoded";
|
|
webRequest.Headers[Constants.CustomerId] = _Tenant;
|
|
|
|
var daten = Encoding.ASCII.GetBytes(postdaten);
|
|
|
|
webRequest.ContentLength = daten.Length;
|
|
|
|
var requestStream = webRequest.GetRequestStream();
|
|
|
|
requestStream.Write(daten, 0, daten.Length);
|
|
|
|
requestStream.Close();
|
|
|
|
var myHttpWebResponse = (HttpWebResponse)webRequest.GetResponse();
|
|
|
|
var responseStream = myHttpWebResponse.GetResponseStream();
|
|
|
|
var myStreamReader = new StreamReader(responseStream, Encoding.Default);
|
|
|
|
var pageContent = myStreamReader.ReadToEnd();
|
|
|
|
if (!string.IsNullOrEmpty(pageContent))
|
|
{
|
|
var jsonDaten = JsonConvert.DeserializeObject<UserDaten>(pageContent);
|
|
|
|
if (jsonDaten.Success)
|
|
{
|
|
foreach (var datev in jsonDaten.Response)
|
|
{
|
|
_AuthToken = datev.Value.Token;
|
|
_UserId = datev.Value.Oid;
|
|
}
|
|
|
|
_LoggedInUser = jsonDaten;
|
|
|
|
_ownchatVerbindungsaufbauOK = true;
|
|
}
|
|
else
|
|
{
|
|
if (jsonDaten.Errors.chatcode_failed != null)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + jsonDaten.Errors.chatcode_failed[0] + "\nBitte überprüfen Sie die Anmeldeinformationen.", "Info", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}else if (jsonDaten.Errors.login_failed != null)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + jsonDaten.Errors.login_failed[0] + "\nBitte überprüfen Sie die Anmeldeinformationen.", "Info", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
_ownchatVerbindungsaufbauOK = false;
|
|
}
|
|
}
|
|
|
|
myStreamReader.Close();
|
|
responseStream.Close();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler:" + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void GetGroups()
|
|
{
|
|
try
|
|
{
|
|
var endurl = _Url + Constants.GetChatGroupsUrl;
|
|
|
|
var request = WebRequest.Create(endurl);
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
request.Headers[Constants.Token] = _AuthToken;
|
|
request.Headers[Constants.CustomerId] = _Tenant;
|
|
|
|
using (var response = request.GetResponse())
|
|
{
|
|
var serverResponse = Utils.ReadStream(response);
|
|
|
|
if (!string.IsNullOrEmpty(serverResponse))
|
|
{
|
|
var jsonDaten = JsonConvert.DeserializeObject<ChatGruppenDaten>(serverResponse);
|
|
|
|
_AllGroups = jsonDaten;
|
|
|
|
_gruppeholenverbindungsaufbauOK = true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Maximal erlaubte File Größe
|
|
|
|
public long GetMaiximumAllowedFileUploadSize(string pToken, string pCustomerId)
|
|
{
|
|
long maximumFileSize;
|
|
|
|
try
|
|
{
|
|
var endurl = ServerUrl + Constants.MaxUploadFileSizeUrl;
|
|
|
|
var request = WebRequest.Create(endurl);
|
|
request.Headers[Constants.Token] = pToken;
|
|
request.Headers[Constants.CustomerId] = pCustomerId;
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
request.Proxy = null;
|
|
|
|
using (var response = request.GetResponse())
|
|
{
|
|
var responseFromServer = Utils.ReadStream(response);
|
|
|
|
var definiti = new {file_upload_max_size = ""};
|
|
|
|
var jsonDatentyp = JsonConvert.DeserializeAnonymousType(responseFromServer, definiti);
|
|
maximumFileSize = Convert.ToInt64(jsonDatentyp.file_upload_max_size);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return maximumFileSize;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|