コード例 #1
0
        private void SignUpButton_Click(object sender, EventArgs e)
        {
            Connect        connect      = Connect.GetInstance();
            DataTable      table        = new DataTable();
            SqlDataAdapter adapter      = new SqlDataAdapter();
            SqlCommand     command      = new SqlCommand();
            SqlCommand     checkCommand = new SqlCommand();
            SqlDataReader  reader;
            bool           isUserExist = false;
            string         sqlQuery    = "select * from Users where email = \'@log\' and password = \'@pass\';";
            string         checkQuery  = "select * from Users;";
            string         pattern     = @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                                         @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$";

            connect.OpenConnection();
            checkCommand.CommandText = checkQuery;
            checkCommand.Connection  = connect.GetConnection();
            reader = checkCommand.ExecuteReader();
            while (reader.Read())
            {
                if (EmailTextBox.Text == reader["email"].ToString())
                {
                    isUserExist = true;
                    reader.Close();
                    break;
                }
            }
            reader.Close();

            command.CommandText = sqlQuery;
            command.Connection  = connect.GetConnection();

            if (Regex.IsMatch(EmailTextBox.Text, pattern, RegexOptions.IgnoreCase))
            {
                if (PasswordTextBox.Text.Length > 4)
                {
                    SaltedHash hashPass = new SaltedHash(PasswordTextBox.Text);

                    command.Parameters.Add("@mail", SqlDbType.VarChar).Value = EmailTextBox.Text;
                    command.Parameters.Add("@pass", SqlDbType.VarChar).Value = hashPass.Hash;
                    command.Parameters.Add("@salt", SqlDbType.VarChar).Value = hashPass.Salt;


                    adapter.SelectCommand = command;
                    adapter.Fill(table);

                    if (!isUserExist)
                    {
                        string     sqlQuery1 = "insert into Users(email, password, salt) values(@mail, @pass, @salt);";
                        SqlCommand command1  = new SqlCommand();
                        command.Connection  = connect.GetConnection();
                        command.CommandText = sqlQuery1;
                        command.ExecuteNonQuery();
                        connect.CloseConnection();

                        OrdinaryUser user     = new OrdinaryUser(this);
                        MainMenu     mainMenu = new MainMenu();
                        Menu         menu     = new Menu(mainMenu, user);
                        mainMenu.SetMenu(menu);
                        MessageBox.Show("Your account was signed up", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                        MessageBox.Show("Welcome to the MoneyManager2020!", "Success");
                        mainMenu.Show();
                    }
                    else
                    {
                        MessageBox.Show("User with this email is already exist!", "Used email", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Password must be longer than 5 symbols!", "Invalid password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Invalid email adress!", "Invalid email", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }