登录之前判断Cookies是否存在,并判断登录错误次数
if (Request.Cookies["loginnum"] != null) { if (Request.Cookies["loginnum"].Value == "3") return Content("<script language='javascript' type='text/javascript'>alert('已经发生 3 次错误!请30分钟再重新查询,谢谢!');history.go(-1);location.reload();</script>"); }
如果登录错误,进入Cookies程序进程,首先判断是否存在Cookies如果没有就创建,创建时赋值为1并设置过期时间为30分钟,注意了!下面一定要加:Response.Cookies.Add(logcookie)
如果第二次登录错误,那么就把登录计数累加,并把累加的新值赋值给当前Cookies,这个时候再重新设置过期时间为30分钟(为了保持时间开始到失效的准确性),注意了!这里还要使用:Response.Cookies.Add(aCookie),不然Cookies在浏览器上永远也不会消失,直到你关闭浏览器的那一时刻起!你要知道用户不会三次无法登录后来关闭一次浏览器来等待30分钟再登录的!这点一定要注意!!!
if (Request.Cookies["loginnum"] == null) { HttpCookie logcookie = new HttpCookie("loginnum", "1"); logcookie.Expires = DateTime.Now.AddMinutes(30); Response.Cookies.Add(logcookie); return Content("<script language='javascript' type='text/javascript'>alert('你输入的客户号不存在!已经发生 1 次错误!\\n如果错误发生 3 次请30分钟再重新查询!');history.go(-1);location.reload();</script>"); } else { int num = int.Parse(Request.Cookies["loginnum"].Value); int counter = num + 1; HttpCookie aCookie = new HttpCookie("loginnum", counter.ToString()); aCookie.Expires = DateTime.Now.AddMinutes(30); Response.Cookies.Add(aCookie); return Content("<script language='javascript' type='text/javascript'>alert('你输入的客户号不存在!已经发生 " + counter + " 次错误!\\n如果错误发生 3 次请30分钟再重新查询!');history.go(-1);location.reload();</script>"); }