asp.net page state management cookie and server state management session

Source: Internet
Author: User
Tags date object copy file system net string variable client
Cookies: A small amount of data stored in the memory of a client-side browser dialog, either in a text file on a file system or in a client browser. When we visit a Web page, when the user requests the page, the application will first check whether the user has logged in before, we can read the cookie to obtain user information to determine whether it continues to access


Record cookie Information
Create a Cookie object with the name User: HttpCookie cookie=new httpcookie ("user");
To assign a value to a cookie, you can only use a string assignment: a cookie. Value= "Chenxiaomei";
If you have more than one string to save, you can do it in the following ways:
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 ("Full value of Cookie:" + cookie.) Value + "<br/>");
Response.Write ("Sex value is:" + cookie["sex"] + "<br/>");
Response.Write ("Age value is:" + cookie["age"] + "<br/>");
}

Delete Cookies
Because cookies are saved on the client, you can have the browser delete cookies for you. Sets the value of the cookie to the previous
A date.
Cookie. Expires = DateTime.Now.AddHours (-1);

Session Object
When the user first establishes a connection to the server, he establishes a session with the server, and the server

It is automatically assigned a SessionID to identify the unique identity of the user.
The specific operation of the session:
Copy CodeThe code is as follows:
Storing information
session["myname"] = "Chenxiaomei";
Get information
String myname = session["MyName"];
Clear session
Session.clear ()//Remove all keys and values from the session state collection
Session.Abandon ()//Cancel current session



The difference between session and Cookie:
The information is stored in a different location and has different time to save
A cookie is a text file that the Web server stores on a Web page client's hard disk. The Web server requests a message from the Web page client to store information that can be stored in a cookie. The information is then sent back to the server whenever the client requests a page from the server.
The session variable will create a Dictionary object on the server for each connection, using a service-side save. The cookie may have a set date that is judged by the year, and the session level variable will expire after the connection times out

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.