2012-03-01 67 views
1

我正在构建登录表单。如果用户尝试使用无效的用户名/密码进行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"; 
    } 
} 
+0

那么什么是不工作? – gideon 2012-03-01 07:13:47

+0

你有我的答案... – 2012-03-01 07:26:46

回答

2

为此,你可以在你的代码或数据库类似

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 
    创建一个表
  1. 当用户登录失败时,三个ti我在你表中输入数据,解释...

  2. 现在,当用户试图登录到系统,您应该检查

    select * from table name [email protected] and GetDate() > DATEADD (mi, LockTime, LockDateTime)

注:查询只是一个suggession这不实际查询为im未添加lockdate + locktime取决于数据库和函数avilable

+0

谢谢,是否有可能没有数据库交互 – Gopesh 2012-03-01 07:41:12

+1

@Gopesh - 正如我writeen你可以在你的代码中创建datatable并将其存储在catch或application.itmes将不会stictly意味着使用数据库 – 2012-03-01 07:43:06

+0

好吧,我会尝试..我想着一个logic.get当前时间,并添加一些秒或分钟它。当新的时间到达按钮必须启用。 – Gopesh 2012-03-01 07:46:52