| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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.IO;
- using System.Net;
- using System.Management;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using System.Data.SQLite;
- namespace BulkPrinting
- {
- public partial class UserLoginForm : Form
- {
- public UserLoginForm()
- {
- InitializeComponent();
- }
- private void btnLogin_Click(object sender, EventArgs e)
- {
- bool LoginSuccessful = false;
- string MaxDBPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Configuration.MaxDataPathName);
- string MaxDBFilePath = Path.Combine(MaxDBPath, Configuration.MaxDBFileName);
- LoginData UserLoginData = new LoginData();
- UserLoginData.VendorId = int.Parse(this.txtVendorID.Text);
- UserLoginData.SerialNumber = Globals.HDDSerialNumber;
- UserLoginData.UserId = int.Parse(this.txtUserID.Text);
- UserLoginData.Username = this.txtUsername.Text;
- UserLoginData.Password = this.txtPassword.Text;
- if (chkOffline.Checked == true) {
- byte[] userKey;
- if (Globals.SessionSalt == null || Globals.SessionIterations == 0){
- MessageBox.Show("It looks like this user hasn't performed an online login today or since the application started. You must perform an online login at least once during the day to use the offline feature. To use offline mode, please remember not to close the application after each session.", "Cannot perform offline login", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- using (var derivedBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(
- UserLoginData.Password + "shie5heeX6pekaehovuS2yu0Ciejah7a",
- Globals.SessionSalt,
- Globals.SessionIterations))
- {
- userKey = derivedBytes.GetBytes(32);
- }
- try
- {
- Globals.SessionDatabasePassword = Utility.AesDecryptBytes(Globals.SessionEncryptedDatabasePassword, userKey);
- }
- catch (Exception) {
- MessageBox.Show("An error occured while logging in in offline mode. Please ensure that you have entered the correct user details and password. If the problem persists, please uncheck 'Offline' before logging in again.", "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (!File.Exists(MaxDBFilePath))
- {
- //Shouldn't get here unless the database is deleted or moved
- MessageBox.Show("A problem has been detected and online initialisation must be performed. You cannot use offline mode until this has been completed.", "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- else
- {
- Globals.DBConnection = new SQLiteConnection(String.Format("Data Source={0};", MaxDBFilePath));
- Globals.DBConnection.SetPassword(Globals.SessionDatabasePassword);
- Globals.DBConnection.Open();
- //Test login success
- Globals.SessionMode = SessionModes.Offline;
- SQLiteCommand SQLCommand = new SQLiteCommand("SELECT Value FROM SessionData WHERE Key = 'SessionDataJson'", Globals.DBConnection);
- Globals.SessionData = JsonConvert.DeserializeObject<OKResponse>(Convert.ToString(SQLCommand.ExecuteScalar()));
- Globals.SessionVoucherKey = Utility.AesDecryptBytes(Globals.SessionData.Credentials.Payload.EncryptedVoucherKey, userKey);
- DateTime ServerDate = Globals.SessionData.Credentials.Payload.Date;
- DateTime Today = DateTime.Now;
- if (ServerDate.Date != Today.Date) {
- MessageBox.Show("It looks like this user hasn't performed an online login today or since the application started. You must perform an online login at least once during the day to use the offline feature. To use offline mode, please remember not to close the application after each session.", "Cannot perform offline login", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- Utility.LogEvent(VendorEvent.VendorEventType.OfflineLogin);
- LoginSuccessful = true;
- }
- }
- else
- {
- if (Utility.Login(UserLoginData, chkOffline.Checked, chkRemember.Checked))
- {
- byte[] userKey;
- LoginSuccessful = true;
- Globals.SessionMode = SessionModes.Online;
- using (var derivedBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(
- UserLoginData.Password + "shie5heeX6pekaehovuS2yu0Ciejah7a",
- Globals.SessionData.Credentials.Salt,
- Globals.SessionData.Credentials.Iterations))
- {
- userKey = derivedBytes.GetBytes(32);
- }
- byte[] signature;
- using (var hmac = new System.Security.Cryptography.HMACSHA256() { Key = userKey })
- {
- signature = hmac.ComputeHash(Encoding.ASCII.GetBytes(Globals.SessionData.Credentials.responsePayload));
- }
- if (!Globals.SessionData.Credentials.Signature.SequenceEqual(signature))
- {
- MessageBox.Show("The login attempt failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else
- {
- Globals.SessionDatabasePassword = Utility.AesDecryptBytes(Globals.SessionEncryptedDatabasePassword, userKey);
- Globals.SessionVoucherKey = Utility.AesDecryptBytes(Globals.SessionData.Credentials.Payload.EncryptedVoucherKey, userKey);
- if (!File.Exists(MaxDBFilePath))
- {
- if (!Directory.Exists(MaxDBPath))
- {
- Directory.CreateDirectory(MaxDBPath);
- }
- SQLiteConnection.CreateFile(MaxDBFilePath);
- Migrations.InitialVersion();
- }
- else
- {
- Globals.DBConnection = new SQLiteConnection(String.Format("Data Source={0};", MaxDBFilePath));
- Globals.DBConnection.SetPassword(Globals.SessionDatabasePassword);
- Globals.DBConnection.Open();
- }
- Utility.SyncLogs(); //Perform log sync before any logging happens to ensure synchronicity with server
- }
- }
- else
- {
- MessageBox.Show("The login attempt failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- if (LoginSuccessful == true)
- {
- //TODO: Remove - for testing purposes only
- /*
- Globals.SessionData.Credentials.Payload.User.Level = (int)UserLevel.CustomUser;
- Globals.SessionData.Credentials.Payload.User.CanPrintOnline = false;
- Globals.SessionData.Credentials.Payload.User.CanPrintOffline = true;
- Globals.SessionData.Credentials.Payload.User.CanReprintOnline = true;
- Globals.SessionData.Credentials.Payload.User.CanReprintOffline = true;
- Globals.SessionData.Credentials.Payload.User.BulkExport = false;
- Globals.SessionData.Credentials.Payload.User.BulkOrder = true;
- Globals.SessionData.Credentials.Payload.User.BulkViewPins = true;
- Globals.SessionData.Credentials.Payload.User.BulkOrderMaxValue = 1000;
- Globals.SessionData.Credentials.Payload.User.BulkExportMaxValue = 0;
- Globals.SessionData.Credentials.Payload.User.OnlinePrintValue = 0;
- Globals.SessionData.Credentials.Payload.User.OfflinePrintValue = 0;
- Globals.SessionData.Credentials.Payload.User.OnlineReprintValue = 0;
- Globals.SessionData.Credentials.Payload.User.OfflineReprintValue = 0;
- */
- string Sql = "DELETE FROM SessionData"; //Destroy stored session data on login - keep in memory until logout
- SQLiteCommand Command = new SQLiteCommand(Sql, Globals.DBConnection);
- Command.ExecuteNonQuery();
- Globals.MaxPrinter = new Printer();
- this.txtPassword.Text = "";
- this.Hide();
- BatchForm MainWindow = new BatchForm();
- MainWindow.Show();
- }
- }
- private void UserLoginForm_Load(object sender, EventArgs e)
- {
- Globals.LogSyncWaiting = false; //Ensure LogSync starts in correct state
- this.lblHDDSerial.Text = Globals.HDDSerialNumber;
- this.txtUsername.Text = Utility.LoadSetting("Username");
- this.txtUserID.Text = Utility.LoadSetting("UserID");
- if (this.txtUsername.Text != "") {
- chkRemember.Checked = true;
- }
- this.txtVendorID.Text = Utility.LoadSetting("VendorID");
- if (this.txtVendorID.Text != "")
- {
- txtVendorID.Enabled = false;
- }
- this.CenterToScreen();
- }
- private void txtVendorID_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- private void UserLoginForm_Shown(object sender, EventArgs e)
- {
- }
- private void UserLoginForm_VisibleChanged(object sender, EventArgs e)
- {
- if (this.Visible == true) {
- List<Form> OpenForms = new List<Form>();
- foreach (Form f in Application.OpenForms)
- OpenForms.Add(f);
- foreach (Form f in OpenForms)
- {
- if (f.Name != "UserLoginForm")
- f.Close();
- }
- }
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- private void label4_Click(object sender, EventArgs e)
- {
- }
- private void label3_Click(object sender, EventArgs e)
- {
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void label5_Click(object sender, EventArgs e)
- {
- }
- private void chkRemember_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void chkOffline_CheckedChanged(object sender, EventArgs e)
- {
- }
- }
- }
|