Obtain user logon count (cookie)

Source: Internet
Author: User

When logging on to the system, obtain the cookie and Add 1 to it. Then save the cookie.

 

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 usercookie = 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);} get it from another page // read cookiestring namecookie = request. cookies ["usercookie"] ["username"]; response. write ("username:" + namecookie); string timecookie = request. cookies ["usercookie"] ["lastvist"]; response. write ("<br> upload access time:" + timecookie); string countcookie = request. cookies ["usercookie"] ["count"]; response. write ("<br> Number of visits:" + countcookie );

Prevent repeated logins to the same account

 

Put it in the place where the login is successful:
String key = textbox1.text; // set the username text box to the cache keyword.
String uer = convert. tostring (Cache [Key]); // read the value of the user in the cache
If (uer = NULL | uer = string. Empty) // checks whether the cache contains user information. If no relevant value exists, it indicates that the user has not logged in.
{
// Define the cache expiration time
Timespan sesstimeout = new timespan (0, 0, system. Web. httpcontext. Current. session. Timeout, 0, 0 );
// Insert a user-related cache value during the first login,
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 response. Write ("<SCRIPT> alert ('your account has logged in! '); Window. Location = 'login. aspx'; </SCRIPT> ");
}

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.