private void TryToLogin() { BackendManager.Password = passwordBox.Text; this.RememberLogin = this.rememberCheck.Checked; bool?loginSuccess = ServerConnections.ServerRequest.LoginWithCredentials(Universal.SystemMACAddress, BackendManager.Password); if (loginSuccess == true) { if (this.RememberLogin) { BackendManager.SaveLoginCookie(); } this.Invoke(new MethodInvoker(delegate { this.parent.Controls.Remove(this); })); BackendManager.LoginNow(User.LoggedIn); } else { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { VisualizingTools.HideWaitingAnimation(); Label errorLabel = new Label(); if (loginSuccess == false) { errorLabel.Text = "Incorrect password!"; } else { errorLabel.Text = "Communication with database failed!"; } errorLabel.ForeColor = Colors.ErrorTextColor; errorLabel.Font = CustomFonts.Smallest; errorLabel.Size = errorLabel.PreferredSize; errorLabel.Location = new Point(this.loginButton.Right - errorLabel.Width + 5, this.loginButton.Bottom + 3); this.Controls.Add(errorLabel); })); } } }
public static void LoginProcessRun() //works as expected { bool?macExists = ServerRequest.MacAddressExists(Universal.SystemMACAddress); if (macExists == false) { if (Universal.ParentForm.InvokeRequired) { Universal.ParentForm.Invoke(new MethodInvoker(delegate { Universal.ParentForm.Controls.Add(new SignupPanel(Universal.ParentForm)); SplashScreen.Instance.Hide(); })); } else { Universal.ParentForm.Controls.Add(new SignupPanel(Universal.ParentForm)); SplashScreen.Instance.Hide(); } return; } else if (macExists == null) { if (Universal.ParentForm.InvokeRequired) { Universal.ParentForm.Invoke(new MethodInvoker(delegate { MessageBox.Show(Universal.ParentForm, "Server connection failed", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); })); } else { MessageBox.Show(Universal.ParentForm, "Server connection failed", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } JObject localCookieJson = Checker.ValidLoginCookieData(Universal.SystemMACAddress); if (localCookieJson != null && (Time.TimeDistanceInMinute(new Time(localCookieJson["last_login_time"].ToString()), Time.CurrentTime) > 4320 || localCookieJson["mac_address"].ToString() != Universal.SystemMACAddress)) { LocalDataFileAccess.EraseCurrentLoginCookie(); localCookieJson = null; } if (localCookieJson == null) { int?userVerifiedOrPasswordIsSet = ServerRequest.UserVerifiedOrPasswordIsSet(Universal.SystemMACAddress); if (userVerifiedOrPasswordIsSet == 0) //0 means user is not verified { if (Universal.ParentForm.InvokeRequired) { Universal.ParentForm.Invoke(new MethodInvoker(ShowEmailVerificationInputPanel)); } else { ShowEmailVerificationInputPanel(); } } else if (userVerifiedOrPasswordIsSet == 1) //1 means user verified but password is not set { if (Universal.ParentForm.InvokeRequired) { Universal.ParentForm.Invoke(new MethodInvoker(ShowPasswordSetupPanel)); } else { ShowPasswordSetupPanel(); } } else if (userVerifiedOrPasswordIsSet == 2) //2 means user is verified and password is set { if (Universal.ParentForm.InvokeRequired) { Universal.ParentForm.Invoke(new MethodInvoker(ShowPasswordEnterPanel)); } else { ShowPasswordEnterPanel(); } } else if (userVerifiedOrPasswordIsSet == null) { Universal.ShowErrorMessage("Server connection failed!"); } } else { bool?loginSuccess = ServerRequest.LoginWithCookie(localCookieJson); if (loginSuccess == true) { BackendManager.Password = localCookieJson["password"].ToString(); BackendManager.SaveLoginCookie(); BackendManager.LoginNow(User.LoggedIn); SplashScreen.Instance.Hide(); } else if (loginSuccess == false) { LocalDataFileAccess.EraseCurrentLoginCookie(); LoginProcessRun(); } else { MessageBox.Show("Server connection failed!"); } } }
private void EventListener(object sender, EventArgs ee) { if (sender == this.bindButton) { string errorMessage = ""; bool allOk = true; if (macAddressBox.Text == null || macAddressBox.Text.Length == 0) { errorMessage += "Invalid MAC Address Input!"; allOk = false; } if (usernameTextBox.Text == null || usernameTextBox.Text.Length == 0) { if (errorMessage.Length > 0) { errorMessage += "\r\n"; } errorMessage += "Username is empty!"; allOk = false; } if (passwordTextBox.Text == null || passwordTextBox.Text.Length == 0) { if (errorMessage.Length > 0) { errorMessage += "\r\n"; } errorMessage += "Password is empty!"; allOk = false; } if (allOk) { VisualizingTools.ShowWaitingAnimation(new Point(this.bindButton.Left, this.bindButton.Bottom + 20), new Size(this.bindButton.Width, this.bindButton.Height / 2), this); BackgroundWorker loaderWorker = new BackgroundWorker(); loaderWorker.DoWork += (s, e) => { try { bool?success = ServerRequest.BindDeviceAndLogin(macAddressBox.Text, usernameTextBox.Text, passwordTextBox.Text); if (success == true) { if (this.loginCheckbox.Checked) { BackendManager.SaveLoginCookie(); } BackendManager.LoginNow(User.LoggedIn); this.Invoke(new Action(() => { VisualizingTools.HideWaitingAnimation(); this.Visible = false; this.parent.Visible = false; this.parent.Dispose(); this.Dispose(); } )); } else if (success == false) { errorMessage = "Invalid username or password!"; this.Invoke(new Action(() => { ShowErrorMessage(errorMessage); } )); } else { errorMessage = "Server connection failed!"; this.Invoke(new Action(() => { ShowErrorMessage(errorMessage); } )); } } catch { } }; loaderWorker.RunWorkerAsync(); loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); }; } else { ShowErrorMessage(errorMessage); } } else if (sender == this.backButton) { this.parent.Visible = true; this.Dispose(); } }