【发布时间】:2018-02-17 18:19:30
【问题描述】:
我有一个确定密码强度的基本工具(不是我的代码)。当单击button1 时,我更改了代码以从TextBox (textBox1) 检索我们正在测试的密码。但是,标签(结果)只显示弱或无的结果。小麦我在这里做错了吗?如何使标签反映在Enum 函数PasswordScore 中找到的结果?
有没有更简单的方法来确定你们使用过的密码强度?
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
public enum PasswordScore
{
Blank = 0,
VeryWeak = 1,
Weak = 2,
Medium = 3,
Strong = 4,
VeryStrong = 5
}
public static PasswordScore CheckStrength(string password)
{
int score = 0;
if (password.Length == 0)
return PasswordScore.Blank;
if (password.Length < 4)
return PasswordScore.VeryWeak;
if (password.Length >= 8)
score++;
if (password.Length >= 12)
score++;
if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript).Success)
score++;
if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript).Success &&
Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript).Success)
score++;
if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript).Success)
score++;
return (PasswordScore)score;
}
public void button1_Click(object sender, EventArgs e)
{
String password = textBox1.Text; // Substitute with the user input string
PasswordScore passwordStrengthScore = Form7.CheckStrength(password);
switch (passwordStrengthScore)
{
case PasswordScore.Blank:
case PasswordScore.VeryWeak:
case PasswordScore.Weak:
// Show an error message to the user
break;
case PasswordScore.Medium:
case PasswordScore.Strong:
case PasswordScore.VeryStrong:
// Password deemed strong enough, allow user to be added to database etc
break;
if (passwordStrengthScore == PasswordScore.Blank) { Result.Text = "Blank"; }
if (passwordStrengthScore == PasswordScore.VeryWeak) { Result.Text = "Very Weak - FAIL"; }
if (passwordStrengthScore == PasswordScore.Weak) { Result.Text = "Weak - FAIL"; }
if (passwordStrengthScore == PasswordScore.Medium) { Result.Text = "Medium - Compliant"; }
if (passwordStrengthScore == PasswordScore.Strong) { Result.Text = "Strong - Compliant"; }
if (passwordStrengthScore == PasswordScore.VeryStrong) { Result.Text = "Very Strong - Compliant"; }
}
}
}
【问题讨论】:
-
在
int score = 0;上放置一个断点,然后使用 F10 键逐行遍历函数的其余部分。在观察窗口中关注score。你会看到到底发生了什么。如果这不能解决问题,请在button1_Click中尝试相同的操作。 -
您的代码示例中没有什么可更改的结果 - 您希望它如何更改?
-
@NetMage 我删除了应用 passwordStrengthScore 结果的 if/then 函数,因为它根本不起作用。这就是我发帖的原因。
标签: c# winforms enums passwords