【发布时间】:2017-12-03 16:57:56
【问题描述】:
我正在创建一个登录功能,我想在用户在 3 次尝试后输入错误信息时包含一个锁定功能,它会将他们带到一个空白面板并摆脱登录部分。我正在使用计数器,但它似乎不起作用。如果我输入了错误的信息,它会告诉我我输入了错误的信息,但我不会被锁定
<asp:Panel ID="panel2" runat="server" Wrap="true" Visible="false">
<h2 id="logError" runat="server" visible="false">Error logging in</h2>
<strong>Username</strong><asp:TextBox runat="server" ID="loginName"></asp:TextBox><br />
<strong>Password</strong><asp:TextBox runat="server" TextMode="Password" ID="loginPass"></asp:TextBox> <br />
<asp:Button runat="server" Text="Return" OnClick="ReturnMain" />
<asp:Button runat="server" Text="Log in" OnClick="login" /><br />
<asp:Label ID="lblInfo2" runat="server"></asp:Label>
</asp:Panel>
public void login(Object src, EventArgs e)
{
get_connection();
try
{
connection.Open();
command = new SqlCommand("SELECT * FROM subscribers WHERE Email =@Email and Password = @Password", connection);
command.Parameters.AddWithValue("@Email", loginName.Text);
command.Parameters.AddWithValue("@Password", loginPass.Text);
//Session["User"] = loginName.Text;
//Session["Number"] = attempt;
int attempt = 0;
reader = command.ExecuteReader();
if (reader.HasRows)
{
//notification that the user has logged in
YouHaveLoggedIn.Visible = true;
panel1.Visible = false;
panel2.Visible = false;
panel6.Visible = true;
WishPanel.Visible = true;
}
else
{
attempt++;
lblInfo2.Text = "Attempt count: " + attempt;
logError.Visible = true;
if (attempt >= 3) // lockout function but does not work, unsure why
{
lockedOut.Visible = true;
panel2.Visible = false;
}
}
reader.Close();
}
catch (Exception err)
{
//user did not log in successfully
lblInfo2.Text = "Error reading the database. ";
lblInfo2.Text += err.Message;
}
finally
{
//lblInfo.Text = "good connect. ";
connection.Close();
}
}
我让 lblInfo2 显示尝试次数,但它始终保持在 1。是我的 if else 语句有问题吗?
【问题讨论】:
-
你认为你在请求之间坚持
attempt在哪里?