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;