我正在建立一个登录表单。如果用户尝试使用无效用户名/密码登录3次,则必须在给定的持续时间内禁用提交按钮。
我该怎么做呢?
下面是我的现有代码:
protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;
string username = TextBox1.Text.Trim();
string password = TextBox2.Text.Trim();
String connString = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("Login", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
Session["LoggedIn"] = "correct";
Response.Redirect("WebForm2.aspx", false);
}
else
{
Label1.Visible = true;
Label1.Text = "Wrong user/password";
conn.Close();
}
if (System.Convert.ToInt32(ViewState["Tries"]) == 2)
{
Label1.Text = "Exceeded 3 times Attempts.Please Login after some time";
TextBox1.Enabled = false;
TextBox2.Enabled = false;
Button1.Enabled = false; // Button1 is the submit button
}
else
{
// Otherwise, increment number of tries.
ViewState["Tries"] = System.Convert.ToInt32(ViewState["Tries"]) + 1;
if (System.Convert.ToInt32(ViewState["Tries"]) == 2)
Label1.Text = "Exceeded 3 times Attempts.Please Login after some time";
}
}发布于 2012-03-01 15:18:38
为此,您可以在代码或数据库中创建一个表,如下所示
LockingTime
Userid LockTime LockedDateTime
1 30 01/03/2012 12:30根据此表
UserId = id of the user locked
LockTime - amount of time user Get locked
LockDateTime - DateTime when user account locked当用户登录失败三次时,以explained...
select * from table name userid=@userid and GetDate() > DATEADD (mi, LockTime, LockDateTime)
注意:查询只是一个提示,而不是实际的查询,因为我没有添加锁定日期+锁定时间,这取决于可用的数据库和函数
https://stackoverflow.com/questions/9512053
复制相似问题