我有一个 ASP.NET Web 应用程序,Web.config
文件设置为启用锁定: maxInvalidPasswordAttempts="8"
.
但是,我不想简单地锁定用户,我想在 4 次尝试后显示一条消息,说明在 X 次额外失败尝试后帐户将被锁定。
示例:假设用户有 4 次尝试失败;他们会看到一条消息,警告帐户将被锁定(我不想指定总尝试次数),只是太多的失败将导致帐户被锁定。
在 8 次尝试失败后,用户会收到一条消息,说明该帐户已被锁定。
为此,我决定使用While Loop
,所以我创建了这样的循环:我通常创建一个粗略的草稿,如果代码有效,那么我将其添加到我的项目中。
我已经登录了,其他一切都在工作,只是这个循环。如果我能找到循环出错的地方,那么我可以进行所有数据库检查并锁定帐户。
if (Membership.ValidateUser(txtUserName.Text, txtPassword.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkMemory.Checked);
}
else
{
ltrStatusMsg.Text = "* InValid Login";
}
问题是这段代码在控制台应用程序中工作,但是当我尝试在按钮事件中编写它时它失败了。
static void Main(string[] args)
{
int loginAttempts = 0;
int maxLoginAttempt = 8;
while (loginAttempts < maxLoginAttempt)
{
Console.Write("Enter a number: ");
Console.ReadLine();
if (loginAttempts == 3)
{
loginAttempts++;
Console.Write("Lock Warning :");
}
loginAttempts++;
}
Console.Write("Account Locked: ");
Console.ReadKey();
}
示例:在按钮事件中: *注意:我已多次更改此代码以找到我的问题。*
protected void btnSubmit_Click(object sender, EventArgs e)
{
int loginAttempts = 0;
int maxLoginAttempt = 8;
while (loginAttempts < maxLoginAttempt & txtPassword.Text != "Test")
{
if (loginAttempts == 3)
{
loginAttempts++;
ltrStatusMsg.Text = "Lock Warning";
}
loginAttempts++;
}
ltrStatusMsg.Text = "Account Locked";
}