如何在给定的持续时间内禁用“提交”按钮?

时间:2012-03-01 07:12:13

标签: c# asp.net login

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

1 个答案:

答案 0 :(得分: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. 当用户登录失败三次时,您按照说明在表格中输入数据......

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

    select * from table name userid=@userid and GetDate() > DATEADD (mi, LockTime, LockDateTime)

  3. 注意:查询只是一个建议,这不是实际查询,因为我没有添加lockdate + locktime,这取决于数据库和功能可用