Beispiel #1
0
        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            // Validation
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                MessageBox.Show("Username field cannot be empty."); return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Password field cannot be empty."); return;
            }
            if (string.IsNullOrEmpty(cbRegion.SelectedItem.ToString()))
            {
                MessageBox.Show("Region field cannot be empty."); return;
            }

            // Create rank text
            string rank     = cbRank.GetItemText(cbRank.SelectedItem);
            string division = cbRankDivision.GetItemText(cbRankDivision.SelectedItem);

            string accountRank;

            if (rank == "Unranked" || rank == "Master" || rank == "Grandmaster" || rank == "Challenger")
            {
                accountRank = rank;
            }
            else
            {
                accountRank = rank + " " + division;
            }

            // Create encrypted password
            try
            {
                byte[] salt              = AES.GenerateSalt();
                byte[] passkey           = GetPasskey(txtUsername.Text);
                byte[] encryptedPassword = AES.Encrypt(
                    Encoding.UTF8.GetBytes(txtPassword.Text),
                    salt, passkey);


                // Create account object
                Account acc = new Account(
                    txtUsername.Text,
                    encryptedPassword,
                    salt,
                    txtNickname.Text,
                    cbRegion.SelectedItem.ToString(),
                    accountRank);

                // Add account to the XML document and save
                xml.AddAccount(acc);
                xml.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while adding a new account.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Clear password
            txtPassword.Clear();

            // Refresh tree view
            LoadAccounts();
        }
        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            // Validation
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                MessageBox.Show("Username field cannot be empty."); return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Password field cannot be empty."); return;
            }
            if (string.IsNullOrEmpty(cbRegion.SelectedItem.ToString()))
            {
                MessageBox.Show("Region field cannot be empty."); return;
            }

            // Create encrypted password
            try
            {
                byte[] salt              = AES.GenerateSalt();
                byte[] passkey           = GetPasskey(txtUsername.Text);
                byte[] encryptedPassword = AES.Encrypt(
                    Encoding.UTF8.GetBytes(txtPassword.Text),
                    salt, passkey);


                // Create account object
                Account acc = new Account(
                    txtUsername.Text,
                    encryptedPassword,
                    salt,
                    txtNickname.Text,
                    cbRegion.SelectedItem.ToString());

                // Add account to the XML document and save
                xml.AddAccount(acc);
                xml.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while adding a new account.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Clear password
            txtPassword.Clear();

            // Refresh tree view
            LoadAccounts();

            // Display green text (success)
            var tmr = new Timer()
            {
                Interval = 650
            };

            tmr.Tick += (o, ev) =>
            {
                btnAddAccount.ForeColor = Color.Black;
                tmr.Stop();
            };
            btnAddAccount.ForeColor = Color.Green;
            tmr.Start();
        }