Exemplo n.º 1
0
        private void createUserButton_Click(object sender, EventArgs e)
        {
            string createdUsername = username.Text;
            string createdPassword = password.Text;

            // Check if the username already exists
            try
            {
                var matchingUserTable = dataManager
                                        .Execute($"SELECT * FROM LoginInfo WHERE username = '******'");
                if (matchingUserTable.Rows.Count != 0)
                {
                    MessageBox.Show("That username already exists.");
                    return;
                }
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
                MessageBox.Show("Something went wrong.");
                return;
            }

            // Validate username and password
            if (createdUsername == "" || createdPassword == "")
            {
                // Not enough data; exit
                MessageBox.Show("One of the fields is empty.");
                return;
            }
            else if (!PasswordVerifier.IsValidPassword(createdPassword))
            {
                // Invalid password; exit
                MessageBox.Show("Password must be at least 8 characters and contain at least one number.");
                return;
            }

            // Hash the password for storage
            String hashedPassword = BCrypt.HashPassword(createdPassword, BCrypt.GenerateSalt());

            // Add user to database
            try
            {
                String query = "INSERT INTO LoginInfo (username, password) " +
                               "VALUES ('" + createdUsername + "', '" + hashedPassword + "')";
                dataManager.Execute(query);
                MessageBox.Show("Login credentials added:\n" + createdUsername + "\n" + hashedPassword);

                username.Text = "";
                password.Text = "";
            }
            catch (InvalidOperationException exception)
            {
                Console.WriteLine(exception.Message);
                MessageBox.Show("Fatal Error: Something went wrong. Try again");
            }
        }