설명 없음

UserLoginForm.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12. using System.Management;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Serialization;
  15. using System.Data.SQLite;
  16. namespace BulkPrinting
  17. {
  18. public partial class UserLoginForm : Form
  19. {
  20. public UserLoginForm()
  21. {
  22. InitializeComponent();
  23. }
  24. private void btnLogin_Click(object sender, EventArgs e)
  25. {
  26. bool LoginSuccessful = false;
  27. string MaxDBPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Configuration.MaxDataPathName);
  28. string MaxDBFilePath = Path.Combine(MaxDBPath, Configuration.MaxDBFileName);
  29. LoginData UserLoginData = new LoginData();
  30. UserLoginData.VendorId = int.Parse(this.txtVendorID.Text);
  31. UserLoginData.SerialNumber = Globals.HDDSerialNumber;
  32. UserLoginData.UserId = int.Parse(this.txtUserID.Text);
  33. UserLoginData.Username = this.txtUsername.Text;
  34. UserLoginData.Password = this.txtPassword.Text;
  35. if (chkOffline.Checked == true) {
  36. byte[] userKey;
  37. if (Globals.SessionSalt == null || Globals.SessionIterations == 0){
  38. 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);
  39. return;
  40. }
  41. using (var derivedBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(
  42. UserLoginData.Password + "shie5heeX6pekaehovuS2yu0Ciejah7a",
  43. Globals.SessionSalt,
  44. Globals.SessionIterations))
  45. {
  46. userKey = derivedBytes.GetBytes(32);
  47. }
  48. try
  49. {
  50. Globals.SessionDatabasePassword = Utility.AesDecryptBytes(Globals.SessionEncryptedDatabasePassword, userKey);
  51. }
  52. catch (Exception) {
  53. 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);
  54. return;
  55. }
  56. if (!File.Exists(MaxDBFilePath))
  57. {
  58. //Shouldn't get here unless the database is deleted or moved
  59. 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);
  60. return;
  61. }
  62. else
  63. {
  64. Globals.DBConnection = new SQLiteConnection(String.Format("Data Source={0};", MaxDBFilePath));
  65. Globals.DBConnection.SetPassword(Globals.SessionDatabasePassword);
  66. Globals.DBConnection.Open();
  67. //Test login success
  68. Globals.SessionMode = SessionModes.Offline;
  69. SQLiteCommand SQLCommand = new SQLiteCommand("SELECT Value FROM SessionData WHERE Key = 'SessionDataJson'", Globals.DBConnection);
  70. Globals.SessionData = JsonConvert.DeserializeObject<OKResponse>(Convert.ToString(SQLCommand.ExecuteScalar()));
  71. Globals.SessionVoucherKey = Utility.AesDecryptBytes(Globals.SessionData.Credentials.Payload.EncryptedVoucherKey, userKey);
  72. DateTime ServerDate = Globals.SessionData.Credentials.Payload.Date;
  73. DateTime Today = DateTime.Now;
  74. if (ServerDate.Date != Today.Date) {
  75. 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);
  76. return;
  77. }
  78. Utility.LogEvent(VendorEvent.VendorEventType.OfflineLogin);
  79. LoginSuccessful = true;
  80. }
  81. }
  82. else
  83. {
  84. if (Utility.Login(UserLoginData, chkOffline.Checked, chkRemember.Checked))
  85. {
  86. byte[] userKey;
  87. LoginSuccessful = true;
  88. Globals.SessionMode = SessionModes.Online;
  89. using (var derivedBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(
  90. UserLoginData.Password + "shie5heeX6pekaehovuS2yu0Ciejah7a",
  91. Globals.SessionData.Credentials.Salt,
  92. Globals.SessionData.Credentials.Iterations))
  93. {
  94. userKey = derivedBytes.GetBytes(32);
  95. }
  96. byte[] signature;
  97. using (var hmac = new System.Security.Cryptography.HMACSHA256() { Key = userKey })
  98. {
  99. signature = hmac.ComputeHash(Encoding.ASCII.GetBytes(Globals.SessionData.Credentials.responsePayload));
  100. }
  101. if (!Globals.SessionData.Credentials.Signature.SequenceEqual(signature))
  102. {
  103. MessageBox.Show("The login attempt failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  104. }
  105. else
  106. {
  107. Globals.SessionDatabasePassword = Utility.AesDecryptBytes(Globals.SessionEncryptedDatabasePassword, userKey);
  108. Globals.SessionVoucherKey = Utility.AesDecryptBytes(Globals.SessionData.Credentials.Payload.EncryptedVoucherKey, userKey);
  109. if (!File.Exists(MaxDBFilePath))
  110. {
  111. if (!Directory.Exists(MaxDBPath))
  112. {
  113. Directory.CreateDirectory(MaxDBPath);
  114. }
  115. SQLiteConnection.CreateFile(MaxDBFilePath);
  116. Migrations.InitialVersion();
  117. }
  118. else
  119. {
  120. Globals.DBConnection = new SQLiteConnection(String.Format("Data Source={0};", MaxDBFilePath));
  121. Globals.DBConnection.SetPassword(Globals.SessionDatabasePassword);
  122. Globals.DBConnection.Open();
  123. }
  124. Utility.SyncLogs(); //Perform log sync before any logging happens to ensure synchronicity with server
  125. }
  126. }
  127. else
  128. {
  129. MessageBox.Show("The login attempt failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  130. }
  131. }
  132. if (LoginSuccessful == true)
  133. {
  134. //TODO: Remove - for testing purposes only
  135. /*
  136. Globals.SessionData.Credentials.Payload.User.Level = (int)UserLevel.CustomUser;
  137. Globals.SessionData.Credentials.Payload.User.CanPrintOnline = false;
  138. Globals.SessionData.Credentials.Payload.User.CanPrintOffline = true;
  139. Globals.SessionData.Credentials.Payload.User.CanReprintOnline = true;
  140. Globals.SessionData.Credentials.Payload.User.CanReprintOffline = true;
  141. Globals.SessionData.Credentials.Payload.User.BulkExport = false;
  142. Globals.SessionData.Credentials.Payload.User.BulkOrder = true;
  143. Globals.SessionData.Credentials.Payload.User.BulkViewPins = true;
  144. Globals.SessionData.Credentials.Payload.User.BulkOrderMaxValue = 1000;
  145. Globals.SessionData.Credentials.Payload.User.BulkExportMaxValue = 0;
  146. Globals.SessionData.Credentials.Payload.User.OnlinePrintValue = 0;
  147. Globals.SessionData.Credentials.Payload.User.OfflinePrintValue = 0;
  148. Globals.SessionData.Credentials.Payload.User.OnlineReprintValue = 0;
  149. Globals.SessionData.Credentials.Payload.User.OfflineReprintValue = 0;
  150. */
  151. string Sql = "DELETE FROM SessionData"; //Destroy stored session data on login - keep in memory until logout
  152. SQLiteCommand Command = new SQLiteCommand(Sql, Globals.DBConnection);
  153. Command.ExecuteNonQuery();
  154. Globals.MaxPrinter = new Printer();
  155. this.txtPassword.Text = "";
  156. this.Hide();
  157. BatchForm MainWindow = new BatchForm();
  158. MainWindow.Show();
  159. }
  160. }
  161. private void UserLoginForm_Load(object sender, EventArgs e)
  162. {
  163. Globals.LogSyncWaiting = false; //Ensure LogSync starts in correct state
  164. this.lblHDDSerial.Text = Globals.HDDSerialNumber;
  165. this.txtUsername.Text = Utility.LoadSetting("Username");
  166. this.txtUserID.Text = Utility.LoadSetting("UserID");
  167. if (this.txtUsername.Text != "") {
  168. chkRemember.Checked = true;
  169. }
  170. this.txtVendorID.Text = Utility.LoadSetting("VendorID");
  171. if (this.txtVendorID.Text != "")
  172. {
  173. txtVendorID.Enabled = false;
  174. }
  175. this.CenterToScreen();
  176. }
  177. private void txtVendorID_KeyPress(object sender, KeyPressEventArgs e)
  178. {
  179. if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
  180. {
  181. e.Handled = true;
  182. }
  183. }
  184. private void UserLoginForm_Shown(object sender, EventArgs e)
  185. {
  186. }
  187. private void UserLoginForm_VisibleChanged(object sender, EventArgs e)
  188. {
  189. if (this.Visible == true) {
  190. List<Form> OpenForms = new List<Form>();
  191. foreach (Form f in Application.OpenForms)
  192. OpenForms.Add(f);
  193. foreach (Form f in OpenForms)
  194. {
  195. if (f.Name != "UserLoginForm")
  196. f.Close();
  197. }
  198. }
  199. }
  200. private void pictureBox1_Click(object sender, EventArgs e)
  201. {
  202. }
  203. private void label4_Click(object sender, EventArgs e)
  204. {
  205. }
  206. private void label3_Click(object sender, EventArgs e)
  207. {
  208. }
  209. private void label2_Click(object sender, EventArgs e)
  210. {
  211. }
  212. private void label1_Click(object sender, EventArgs e)
  213. {
  214. }
  215. private void label5_Click(object sender, EventArgs e)
  216. {
  217. }
  218. private void chkRemember_CheckedChanged(object sender, EventArgs e)
  219. {
  220. }
  221. private void chkOffline_CheckedChanged(object sender, EventArgs e)
  222. {
  223. }
  224. }
  225. }