asp.net cookies record the number of user logins and prevent duplicate logins to the same account

Source: Internet
Author: User
Tags datetime httpcontext

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");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.