2012-03-01 59 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