133 lines
4.4 KiB
C#
133 lines
4.4 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AyaNovaQBI
|
|
{
|
|
public partial class auth : Form
|
|
{
|
|
public auth()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void auth_Load(object sender, EventArgs e)
|
|
{
|
|
//case 3875
|
|
var version = Environment.OSVersion.Version;
|
|
if (version < new Version(10, 0))//anything less than windows 10
|
|
{
|
|
System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
|
|
}
|
|
|
|
edUserName.Text = Properties.Settings.Default.authuser;
|
|
edServerUrl.Text = Properties.Settings.Default.serverurl;
|
|
|
|
#if (DEBUG)
|
|
edUserName.Text = "Accounting";
|
|
edPassword.Text = "Accounting";
|
|
#endif
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(edUserName.Text))
|
|
edPassword.Focus();
|
|
}
|
|
|
|
private async void btnTest_Click(object sender, EventArgs e)
|
|
{
|
|
if (!ValidateAndCleanServerAddress()) return;
|
|
btnLogin.Enabled = btnTest.Enabled = false;
|
|
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
if (result == "OK")
|
|
{
|
|
MessageBox.Show("Server URL is GOOD!");
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
|
}
|
|
}
|
|
|
|
private async void btnLogin_Click(object sender, EventArgs e)
|
|
{
|
|
if (!ValidateAndCleanServerAddress()) return;
|
|
btnLogin.Enabled = btnTest.Enabled = false;
|
|
|
|
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
|
|
if (result != "OK")
|
|
{
|
|
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text);
|
|
if (!res)
|
|
{
|
|
MessageBox.Show("Login failed");
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
this.DialogResult = DialogResult.Cancel;
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Error attempting to login:\r\n" + ex.Message);
|
|
this.DialogResult = DialogResult.Cancel;
|
|
return;
|
|
}
|
|
|
|
// MessageBox.Show($@"TEST - LOGIN SUCCEEDED: \r\
|
|
//AyaNovaUserName: {util.AyaNovaUserName}\r\n
|
|
//JWT: {util.JWT}\r\n
|
|
//AyaNovaUserRoles: {util.AyaNovaUserRoles}\r\n
|
|
//AyaNovaUserType: {util.AyaNovaUserType}");
|
|
|
|
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
//successful login, persist server setting
|
|
Properties.Settings.Default.authuser = edUserName.Text;
|
|
Properties.Settings.Default.serverurl = edServerUrl.Text;
|
|
Properties.Settings.Default.Save();
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
this.Close();
|
|
}
|
|
|
|
private bool ValidateAndCleanServerAddress()
|
|
{
|
|
var serverUrl = edServerUrl.Text;
|
|
Uri u;
|
|
try
|
|
{
|
|
u = new Uri(serverUrl);
|
|
var scheme = u.Scheme;
|
|
var host = u.Host;
|
|
var port = u.Port;
|
|
|
|
edServerUrl.Text = scheme + "://" + host + ":" + port + "/" + util.API_BASE_ROUTE;
|
|
|
|
//client url for notification default links, help links etc
|
|
if ((scheme == "https" && port == 443) || (scheme == "http" && port == 80))
|
|
util.GuessClientUrl = scheme + "://" + host + "/";
|
|
else
|
|
util.GuessClientUrl = scheme + "://" + host + ":" + port + "/";
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Server address not valid\n" + ex.Message);
|
|
return false;
|
|
}
|
|
|
|
//ok, it's some kind of url, now format it for api access
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|
|
}
|