繁体   English   中英

在ASP.NET中尝试3次无效密码后如何阻止用户名

[英]How To Block The UserName After 3 Invalid Password Attempts IN ASP.NET

我使用以下代码检查用户名和密码。 并且我想在尝试3次无效密码后阻止该用户名。 我应该在代码中添加什么

MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
        Byte[] hashedDataBytes;
        UTF8Encoding encoder = new UTF8Encoding();
        hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(TextBox3.Text));
        StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
        foreach (Byte b in hashedDataBytes)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        string hash = hex.ToString();

    SqlConnection con = new SqlConnection("Data Source=Shihab-PC;Initial Catalog=test;User ID=SOMETHING;Password=SOMETHINGELSE");
    SqlDataAdapter ad = new SqlDataAdapter("select password from Users where UserId='" + TextBox4.Text + "'", con);
    DataSet ds = new DataSet();
    ad.Fill(ds, "Users");

    SqlDataAdapter ad2 = new SqlDataAdapter("select UserId from Users ", con);
    DataSet ds2 = new DataSet();
    ad2.Fill(ds2, "Users");
    Session["id"] = TextBox4.Text.ToString();


    if ((string.Compare((ds.Tables["Users"].Rows[0][0].ToString()), hash)) == 0)
    {
        if (string.Compare(TextBox4.Text, (ds2.Tables["Users"].Rows[0][0].ToString())) == 0)
        {
                            Response.Redirect("actioncust.aspx");

        }
        else
        {

            Response.Redirect("actioncust.aspx");


        }
    }
    else
    {
        Label2.Text = "Invalid Login";
    }

    con.Close();

}

您应该使用ASP.Net成员资格 ,它是开箱即用且实际上是安全的。

正如SLaks所建议的那样,最好使用ASP.Net成员资格 如果您在Google中搜索网络上的教程,则很多。

其次,您不必编写两个查询。

您可以使用Select UserId from Users where UserId=@id and password=@pass ,然后使用数据集的count属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM