好的,所以我有一个任务,我必须让客户端尝试输入密码3次,如果他在3次中没有输入正确的密码,它会将他重定向到另一个页面,问题是我不知道如何使用会话,我如何像++或其他东西。
Session["counter"] = 0;我正在尝试做以下几件事:
Session["counter"]++;如何检测客户端是否尝试输入密码3次?谢谢
发布于 2012-04-04 03:01:04
int counter=1;
Session["counter"]=counter;当您想要更新该值时,读取该值并将其转换为int,然后递增,保存回来
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
counter++;
Session["counter"]=counter;编辑:根据注释,这是您可以检查计数器值的方式。我将检查封装在两个方法中,以设置和获取,您甚至可以像其他人提到的那样使用属性。
private int GetLoginAttempts()
{
int counter=0;
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
return counter;
}
private void IncreaseLoginAttempts()
{
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
counter++;
Session["counter"]=counter;
}当用户尝试登录(在您的按钮单击/操作方法中)时,检查当前值
if(GetLoginAttempts()==3)
{
//This means user already tried 3 times, show him a message !
}
else
{
//Do the login process, If login fails, increase the counter
IncreaseLoginAttempts()
}发布于 2012-04-04 03:00:07
尝尝这个。
int counter = Int32.Parse(Session["counter"].ToString()); //Session["counter"] may be null
Session["counter"] = ++counter;发布于 2012-04-04 03:04:06
您可以通过将其包装在属性中来实现它,如下所示:
public int PasswordAttempts
{
get
{
if (Session["PasswordAttempts"] == null)
Session["PasswordAttempts"] = 0;
return (int)Session["PasswordAttempts"];
}
set
{
Session["PasswordAttempts"] = value;
}
}
protected void Submit_Click(object sender, EventArgs e)
{
PasswordAttempts++;
}https://stackoverflow.com/questions/9999600
复制相似问题