我想在程序中使用强密码。因此,我搜索了强力的密码验证,但是我不知道如何在保存时调用该函数。
这是我要用于密码验证的功能:
private bool ValidatePassword(string password, out string ErrorMessage)
{
var input = password;
ErrorMessage = string.Empty;
if (string.IsNullOrWhiteSpace(input))
{
throw new Exception("Password should not be empty");
}
var hasNumber = new Regex(@"[0-9]+");
var hasUpperChar = new Regex(@"[A-Z]+");
var hasMiniMaxChars = new Regex(@".{8,15}");
var hasLowerChar = new Regex(@"[a-z]+");
var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
if (!hasLowerChar.IsMatch(input))
{
ErrorMessage = "Password should contain At least one lower case letter";
return false;
}
else if (!hasUpperChar.IsMatch(input))
{
ErrorMessage = "Password should contain At least one upper case letter";
return false;
}
else if (!hasMiniMaxChars.IsMatch(input))
{
ErrorMessage = "Password should not be less than or greater than 12 characters";
return false;
}
else if (!hasNumber.IsMatch(input))
{
ErrorMessage = "Password should contain At least one numeric value";
return false;
}
else if (!hasSymbols.IsMatch(input))
{
ErrorMessage = "Password should contain At least one special case characters";
return false;
}
else
{
return true;
}
}
保存按钮代码:
private void btnSave_Click(object sender, EventArgs e) {
var data = DBConnection.DBConnect();
SqlCommand cmd = new SqlCommand("Insert_Users", data);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StaffName", SqlDbType.VarChar).Value = txtStaffName.Text;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUsername.Text;
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text;
cmd.Parameters.Add("@Phoneno", SqlDbType.NVarChar).Value = txtPhoneNo.Text;
cmd.Parameters.Add("@Admin", SqlDbType.Char).Value = chkIsAdmin.CheckState == CheckState.Checked ? 1 : 0;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
MessageBox.Show("Saved Sucessfully");
LoadUsers();
}
请问有人可以如何使用此功能执行密码验证吗?
答案 0 :(得分:0)
也许我没听懂问题,但是就这么简单吗?:
private void btnSave_Click(object sender, EventArgs e) {
var error = string.Empty;
if(ValidatePassword(txtPassword.Text, error))
{
var data = DBConnection.DBConnect();
SqlCommand cmd = new SqlCommand("Insert_Users", data);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StaffName", SqlDbType.VarChar).Value = txtStaffName.Text;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUsername.Text;
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text;
cmd.Parameters.Add("@Phoneno", SqlDbType.NVarChar).Value = txtPhoneNo.Text;
cmd.Parameters.Add("@Admin", SqlDbType.Char).Value = chkIsAdmin.CheckState == CheckState.Checked ? 1 : 0;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
MessageBox.Show("Saved Sucessfully");
LoadUsers();
}
else
{
throw new Exception(error); // Or Console.WriteLine(error) or whatever
}
}