Asp.net Page Status Management cookie and server status management Session

Source: Internet
Author: User

Cookie: a small amount of data stored in the Client File System text file or client browser Dialog Memory. When a user requests a webpage, the application first checks whether the user has logged on to the webpage, we can read the Cookie to obtain user information to determine whether to allow it to continue accessing.


Record Cookie Information
Create a Cookie object named "user": HttpCookie cookie = new HttpCookie ("user ");
Assign values to cookies. Only strings can be used: Cookie. Value = "chenxiaomei ";
If you want to save multiple strings, you can do this as follows:
Cookie ["sex"] = "female ";
Cookie. Values. Add ("age", "18 ");


Read Cookie Information
Copy codeThe Code is as follows:
HttpCookie cookie = Request. Cookies ["user"];
If (null = cookie)
{
Response. Write ("No cookie found ");
}
Else
{
Response. Write ("All cookie values:" + cookie. Value + "<br/> ");
Response. Write ("sex value:" + cookie ["sex"] + "<br/> ");
Response. Write ("age value:" + cookie ["age"] + "<br/> ");
}

Delete Cookie
Because the Cookie is stored on the client, the browser can delete the Cookie for you. Set the Cookie value to the previous
A date.
Cookie. Expires = DateTime. Now. AddHours (-1 );

Session Object
When the user establishes a connection with the server for the first time, he creates a Session with the server, and the server will

It is automatically assigned a SessionID to identify the unique identity of the user.
Specific Session operations:
Copy codeThe Code is as follows:
// Store information
Session ["myname"] = "chenxiaomei ";
// Obtain information
String myname = Session ["myname"];
// Clear the session
Session. Clear (); // remove all keys and values from the Session state set
Session. Abandon (); // cancel the current Session



Difference between Session and Cookie:
The storage location of the information varies with the storage time.
Cookie is a text file stored by the webpage server on the hard disk of the webpage client. The webpage server sends a request to the webpage client to store a piece of information, which can be stored in cookies. When the client requests a page from the server, the information is sent back to the server.
Session variables will create a dictionary object for Each connection on the server, which is saved on the server. Cookie may have an expiration date determined by year, month, and day, and Session-level variables expire after connection timeout.

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.