我是新手,我只想知道是否有人熟悉限制用戶最多隻能登錄3次,但是失敗。登錄工作正常,但是當我嘗試使用不正確的密碼訪問網絡時,計數器不起作用。有人能幫我嗎 。這裏是我的代碼:ASP.NET限制登錄重試
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(GetConnectionString());
con.Open();
SqlCommand cmd = new SqlCommand("CheckMember",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@userName",txtUsername.Text.ToString());
SqlParameter p2 = new SqlParameter("@password",txtPassword.Text.ToString());
SqlParameter p3 = new SqlParameter("@accountNo",txtAcctNo.Text.ToString());
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
SqlDataReader lmsreader = cmd.ExecuteReader();
if (lmsreader.HasRows)
{
lmsreader.Read();
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text.ToString(), false);
// FormsAuthentication.RedirectFromLoginPage(txtUsername.Text.ToString(), chkboxCookie.Checked);
lblError.Text = "You got it!";
Response.Redirect("MyAccount.aspx");
con.Close();
}
else
{
this.lblError.Text = "Invalid username, password or account number.<br> Please try again.";
this.lblError.ForeColor = System.Drawing.Color.Red;
object FailedLoginCounter = this.Page.Cache["UserKey_" + this.txtUsername.Text];
if (FailedLoginCounter == null)
{
FailedLoginCounter = 0;
}
this.Page.Cache["UserKey_" + this.txtUsername.Text] = (int)FailedLoginCounter + 1;
if (((int)this.Page.Cache["UserKey_" + this.txtUsername.Text]) == 3)
{
SqlConnection conect = new SqlConnection(GetConnectionString());
SqlCommand commander = new SqlCommand("MemberIsBlocked", con);
commander.CommandType = CommandType.StoredProcedure;
conect.Open();
SqlParameter puser = new SqlParameter("@username", txtUsername.Text.ToString());
cmd.ExecuteNonQuery();
conect.Close();
lblError.Text = "You are Temporarily Blocked for <br> Exceeding Max Number of Login Attempts.";
}
}
}
catch
{
}
finally
{
}
}
public string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["ConnectionServices"].ConnectionString;
}
您的幫助將不勝感激!謝謝!
嘗試使用'Session [「User_Key」+ ...]'而不是'this.Page.Cache [「User_Key」+ ...]'。 – 2012-03-16 05:32:34
當你說'櫃檯不工作'時,你究竟是什麼意思?你是否已經通過調試器瀏覽了這段代碼,看看到底發生了什麼? – patmortech 2012-03-16 05:35:26
@UweKeim我幾秒鐘前做了,但還沒有工作。 – Dhenn 2012-03-16 05:44:00