ASP. net2.0 secrets Reading Notes 5-cookie for maintaining Application Status

Source: Internet
Author: User

BrowserCookiesIt is used to associate some text to every user on the website.

Use a browserCookie

WhenWebCreateCookieWhen an additionalHTTPThe header is sent to the browser when the browser displays the page.HTTPThe header is in the following format:

Set-Cookie:Message = Hello

Set-CookieFirst, let the browser createMessageOfCookieInclude ValueHello.

Create in a browserCookieLater, it starts from the same applicationProgramTheCookieToWebServer.

OneCookieIs a piece of text,CookieIt is used only to store string values.

actually, we can store two types of cookies , one is session cookie , persistence cookie . Session cookie only stored in memory, closed browser disappears, persistent cookie is saved to your computer and can be persisted. Note: persistence cookie is independent of browsers, that is, the cookies cannot be shared if they are stored in different locations.

In addition,CookieThe stored information is in plain text, so do not save sensitive informationCookie.

CookieSecurity restrictions

CookieIndependent from browsers

CookieIndependent from Domain Name

One domain nameCookieThe total size cannot exceed4096 bytes. This size limit contains allCookieIncluding the name and value. Most browsers limit a domain name to no more20ItemsCookie.

CreateCookie

You canResponse. CookiesAdd setCookieCreate a newCookie.Response. CookiesThe set contains allWebThe server sendsCookie.

Response. Cookies ["message"]. value = txtcookievalue. text;

AboveCodeOnly one session cookie is created. disabling the browser disappears. To create a persistent cookie, you must specify an expiration time for the cookie.

Response. Cookies ["counter"]. value = counter. tostring ();
Response. Cookies ["counter"]. expires = datetime. Now. addyears (2 );

ReadCookie

Request. CookiesSet readableCookie.

If (request. Cookies ["message"]! = NULL)
Lblcookievalue. Text = request. Cookies ["message"]. value;

SetCookieAttribute

HttpcookieClass representativesCookieWhen you create or readCookieThe following attributes can be used:

Domain-- Set AssociationCookieDomain name;

Expires-- Create a persistence by specifying an expiration timeCookie;

Haskeys-- SpecifiesCookieWhether it is a multi-valueCookie;

HTTPOnly-- Used to avoidCookieQuiltJavascriptAccess;

Name--CookieName;

Path-- AssociateCookiePath. The default value is/;

Value-- Read/writeCookieValue;

Secure-- Used to specifyCookieSecuritySocketLayer connection transmission;

Values-- Use multi-valueCookieUsed to read and write specific values.

DeleteCookie

SetCookieOfExpiresIs the past time.

Use multi-valueCookie

Because a single domain name browser cannot store more20ItemsCookie, You can create multiple valuesCookie. Multi-ValueCookieIs a single sub-KeyCookie. You can create any number of subkeys as needed.

Response. Cookies ["Preferences"] ["firstname"] = txtfirstname. text;

Response. Cookies ["Preferences"] ["lastname"] = txtlastname. text;

Response. Cookies ["Preferences"] ["favoritecolor"] = txtfavoritecolor. text;

Response. Cookies ["Preferences"]. expires = datetime. maxvalue;

 

 

 

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.