ASP tutorial. NET Cookies record the number of user logons and prevent the same account from repeatedly logging on
Place on the successful landing:
string key = TextBox1.Text///Username text box set to cache keyword
String uer = Convert.ToString (Cache[key]); Read the user's corresponding value in the cache
if (Uer = null | | uer = = String.Empty)//Judge whether the cache has a user's information, if there is no relevant value, indicating that the user did not log in
{
Define cache Expiration Time
TimeSpan sesstimeout = new TimeSpan (0, 0, system.web.httpcontext.current.session.timeout, 0, 0);
Insert a user-related cache value on the first landing
Httpcontext.current.cache.insert (key, key, NULL, DateTime.MaxValue, Sesstimeout, System.web.caching.cacheitempriority.notremovable, NULL);
session["Adminid"] = TextBox1.Text;
Response.Redirect ("main.aspx");
}
Else
{
Repeat Login Response.Write (' Your account has landed! ') ("<script>alert"); window.location= ' login.aspx ';</script> ");
}
Number plus 1
if (request.cookies["logincount"] = = null)
{
HttpCookie c= New HttpCookie ("Logincount");;
response.cookies["Logincount"].value = "1";
response.cookies["Logincount"].expires = datetime.now.adddays (1);
Response.Cookies.Add (c);
}
Else
{
int count = Convert.ToInt32 (request.cookies["Logincount"].value) + 1;
response.cookies["Logincount"].value = count.tostring ();
}
if (request.cookies["Usercookie"] = = null)
{
HttpCookie usercookie = new HttpCookie ("Usercookie");
usercookie.values["username"] = userinfo.username.tostring ();
usercookie.values["lastvist"] = DateTime.Now.ToString ();
usercookie.values["Count" = "1";
Usercookie.expires = datetime.now.adddays (30);
Response.Cookies.Add (Usercookie);
}
Else
{
int counter = Convert.ToInt32 (request.cookies["Usercookie"] ["count"]) + 1;
HttpCookie UserC Ookie = new HttpCookie ("Usercookie");
usercookie.values["username"] = userinfo.username.tostring ();
usercookie.values["lastvist"] = DateTime.Now.ToString ();
usercookie.values["Count"] = counter.tostring ();
Usercookie.expires = datetime.now.adddays (30);
Response.Cookies.Add (Usercookie);
}
Fetch on another page
//Read Cookie
String namecookie = request.cookies["Usercookie" ["username"];
Response.Write (" User name: "+ Namecookie";
String Timecookie = request.cookies["Usercookie" ["lastvist"];
Response.Write ("<br> Upload access time:" + Timecookie);
String Countcookie = request.cookies["Usercookie" ["Count"];
Response.Write ("<br> Hits: + Countcookie");