Web --- & gt; Cookie and Session, web --- cookie

Source: Internet
Author: User

Web ---> Cookie and Session, web --- cookie


1. cookie
1. Cookies exist in the process memory of the client (browser) and on the hard disk of the machine on which the client is located.
2. Cookies can only store a small amount of text, about 4 kb
3. Cookies cannot be shared between different browsers.



3. Demonstration of cookie object Creation
// 3.0 instantiate the cookie object
HttpCookie cookie = new HttpCookie ("uid", uid );
// Tell the browser to send the cookie to the server only when the page in the specified path is requested. If no cookie is specified, the default value is/
Cookie. Path = "/P01cookiedemo /";

// If the Expires parameter is set, the browser is notified to store the cookie to the hard disk,
Cookie. Expires = DateTime. Now. AddDays (3 );

// Set Domain to tell the browser that the cookie is sent to the server only when a page under www.c11.com is requested
Cookie. Domain = "www.c11.com ";

// 4.0 Add the cookie to the response header and send it back to the browser for saving,
Response. Cookies. Add (cookie );

4. Get the cookie value that the browser sends to the server through the request header. uid: the cookie key, which can be defined by the programmer.
If (Request. Cookies ["uid"]! = Null)
{
Request. Cookies ["uid"]. Value;
}

5. How to clear the cookie code:
HttpCookie cookie = Request. Cookies ["uid"];
Cookie. Expires = DateTime. Now. AddYears (-1 );
Cookie. Path = "/P01cookiedemo /";

Response. Cookies. Add (cookie );

5. Single Sign-on


2. GMT, UTC time, zero time zone, East District + hour West District:-hour
In linux, the default time is UTC.

3. Session
1. There are servers and website processes in memory
2. When the session is set for the first time, a session object will be instantiated in the session pool, with the value of asp.net _ sessionid as the key, and the key will be in the form of a cookie.
Save to client memory
3. The session scope only exists in the session of the current browser. When the browser is closed, the sessionid will be lost, but the session object on the server will be recycled 20 minutes later.


Clear Session: context. Session ["name"] = null;
Context. Session. Abandon ();

4. Add <sessionState timeout = "5" cookieless = "true"> </sessionState> under the <system. web> node in web. config to manually modify session settings
Note:
After the aspx compilation is complete, the interface IRequiresSessionState is automatically implemented, so you can directly use the session
However, for a general processing program, the programmer must manually implement the system. Web. SessionState. IRequiresSessionState interface to use the session object.

You can use
Set: <sessionState cookieless = "true"> </sessionState> to enable the url to pass sessionid to the server, but it is not recommended to use

 

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.