检索哈希密码

时间:2014-03-12 05:59:42

标签: c# winforms ms-access hash passwords

我已经可以将密码设置为未存储为数据库中的原始文本。但是,当我想检索它并在数据库中检查输入的密码时,出现错误

  

值不能为空

在这行代码上:

string verifyHashedPassword = Convert.ToString(Crypto.VerifyHashedPassword(_registration.hashedPassword, this.textBox2.Text));

以下是我用于登录的代码:

  string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data  Source=..\db1.accdb";

  Registration _registration = new Registration();

  private void CheckUserDatabase(object sender, EventArgs e)
    {
        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            string query = "SELECT [Username], [Password], [UserType], [UserStore] FROM [Member] WHERE [Username] = @Username AND [Password] = @Password";

            string verifyHashedPassword = Convert.ToString(Crypto.VerifyHashedPassword(_registration.hashedPassword, this.textBox2.Text));

            conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                cmd.Parameters.Add("@Username", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@Username"].Value = this.textBox1.Text;

                cmd.Parameters.Add("@Password", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@Password"].Value = verifyHashedPassword;

                using (OleDbDataReader dReader = cmd.ExecuteReader())
                {
                    if (dReader.Read())
                    {
                        UserInformation.CurrentLoggedInUser = (string)dReader["Username"];
                        UserInformation.CurrentLoggedInUserType = (string)dReader["UserType"];
                        UserInformation.CurrentLoggedInUserStore = (string)dReader["UserStore"];
                    }

                    else
                    {
                        RecursiveClearTextBoxes(this.Controls);
                    }

                    dReader.Close();
                    conn.Close();
                }
            }
        }
    }

以下是注册代码:

    string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";

    private void AddDatabase(object sender, EventArgs e)
    {
           string query = "INSERT INTO [Member] ([Username], [Password], [UserType],              [UserStore]) VALUES (@Username, @Password, @UserType, @UserStore)";

            string hashedPassword = Crypto.HashPassword(this.textBox2.Text);

            OleDbConnection _conn = new OleDbConnection(connectionString);

            _conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, _conn))
            {
                cmd.Parameters.Add("@Username", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@Username"].Value = this.textBox1.Text;

                cmd.Parameters.Add("@Password", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@Password"].Value = hashedPassword;

                cmd.Parameters.Add("@UserType", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@UserType"].Value = this.textBox3.Text;

                cmd.Parameters.Add("@UserStore", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["@UserStore"].Value = this.textBox4.Text;

                cmd.ExecuteNonQuery();

                DialogResult _dialogResult = MessageBox.Show("Added Successfully", "Success", MessageBoxButtons.OK);

                if (_dialogResult == DialogResult.OK)
                {
                    this.Hide();

                    this.Close();
                }
            }
        }
    }

任何帮助?

你的回答非常感谢!

谢谢

0 个答案:

没有答案