private void btnSave_Click(object sender, EventArgs e) { if (Credential.IsEmailValid(tbEmail.Text) && !DbAction.IsUsernameInDatabase(tbEmail.Text) && !ServerAction.IsUsernameInServer(tbEmail.Text)) { try { var serverSidePass = cbBackUpPassword.Checked ? tbPassword.Text : string.Empty; if (ServerAction.RegisterNewUser(tbEmail.Text, serverSidePass)) { DbAction.AddNewUser(tbEmail.Text, Crypto.MakeHash(tbPassword.Text)); Close(); } else { lblInvalidRegistration.Text = "Could not Register user. Please try again"; lblInvalidRegistration.Visible = true; } } catch (Exception ex) { lblInvalidRegistration.Text = ex.Message; } } else { lblInvalidRegistration.Visible = true; } }
private void btnLogIn_Click(object sender, EventArgs e) { try { var Password = mtbPassword.Text; string dbPassword = string.Empty; var TbUsername = tbUsername.Text; if (DbAction.IsUsernameInDatabase(TbUsername)) { dbPassword = DbAction.GetPasswordByUser(TbUsername); } else { var SrvrSidePass = ServerAction.GetServerSidePass(TbUsername, Password); if (SrvrSidePass != string.Empty) { try { if (DbAction.AddNewUser(TbUsername, SrvrSidePass)) { throw new DatabaseConnectionFailure("Error inserting data into Database!"); } } catch (DatabaseConnectionFailure error) { MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { lblInfo.Text = "Wrong Username/Password"; lblInfo.Visible = true; } } if (Crypto.CompareHash(Password, dbPassword)) { Hide(); frm_main frm_Main = new frm_main(tbUsername.Text, dbPassword); frm_Main.FormClosed += (s, args) => Close(); frm_Main.Show(); } else { throw new Internals.UnauthorizedAccessException("Wrong Username/Password"); } } catch (Internals.UnauthorizedAccessException error) { MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }