Cookie and session are basically used and encapsulated, cookiesession

Source: Internet
Author: User
Tags time 0

Cookie and session are basically used and encapsulated, cookiesession

Cookie:

It is a short text message that is transmitted between the browser and the server when a user requests a page. Each time a user accesses a website, the cookie can contain user information and browsing history. The cookie is generated by the server but belongs to the client, sent to User-Agent (usually a browser). The browser saves the Cookie key/value to a text file in a directory, the Cookie is sent to the server when the same website is requested next time (provided that the browser is set to enable the cookie ).

Basic Syntax:

Compile the cookie storage: --- create a cookie object and assign the custom cookie name HttpCookie mycookie = new HttpCookie ("UserSettings"); User-Defined name; mycookie. values. add ("name1", "value1"); add key-value pairs to the cookie. values. add ("name2", "value2"); in other words, mycookie ["name1"] = "value1"; the same is true for mycookie. expires = DateTime. now. addDays (1d); the expiration time is set to one day Response. cookies. add (mycookie); Note: If the cookie expiration limit is not specified, the Cookie will not be retained on the client computer, this Cookie expires when the user's session expires. Cookie can only store values of the String type. Before storing any non-string values in the Cookie, you must convert them into strings: if (Request. Cookies ["UserSettings"]! = Null) {string getVal = ""; if (Resquest. Cookies ["UserSettings"] ["name1"]! = Null) {getVal = Resquest. cookies ["UserSettings"] ["name1"] ;}// Delete cookie // you only need to set the cookie expiration time; if (Request. cookies ["UserSettings"]! = Null) {HttpCookie myCookie = new HttpCookie ("UserSettings"); myCookie. Expires = DateTime. Now. AddDays (-1d); Response. Cookies. Add (myCookie );}

Cookie is encapsulated into a method:

1. Create a cookie object and assign a value. You can use this method to modify the value of the cookie. Because the expiration time must be set again. 2. strCookieName: cookie Object Name 3. iExpires: cookie effective time 0 indicates a week,-1 indicates permanent, and if the value is greater than 2 or less 60 indicates the number of days. Others indicate the number of minutes. 4 strValue: cookie object value 5 public static void SetCookie (string strCookieName, int iExpires, string strValue) {6 HttpCookie objCookie = new HttpCookie (strCookieName); 7 objCookie. value = System. web. httpUtility. urlEncode (strCookieName); 8 If (iExpires> = 0) {9 s Witch (iExpires> = 0) {10 case 0: 11 objCookie. expires = DateTime. now. addDays (7); break; 12 case-objCookie. expires = DateTime. maxValue; break; 14} 15 default: 16 if (iExpires> = 60) 17 objCookie. expires = DateTime. now. addSeconds (iExpires); 18 else19 objCookie. expires = DateTime. now. addDays (iExpires); 20 21} 22 HttpContext. current. response. cookie. add (objCookie); 23} 24 25 read cookie26 and pass in the same name. 27 read cookie if the value of an object does not exist, return the string "28 public static string Getcookie (string strCookieName) {29 switch (HttpContext. current. request. cookies [strCookieName] = null) {30 case true: 31 strCookieName = String. empty; break; 32 case false: 33 strCookieName = System. web. httpUtility. urlDecode (HttpContext. current. request. cookies [strCookieName]. value); break; 34} 35 return strCookieName; 36}

 

The most common one is session.

Select Encapsulation

// Save the session value // set the session value public static void SetSession (string name, object value) {try {HttpContext. current. session [name] = value;} catch (Exception ex) {WriteErrorLog (ex, "HelperError")} // Error Log public static void WriteErrorLog (Exception ex, string ErroType) {if (ex = null) return; System. text. stringBuilder sbui = new StringBuilder (); string datetime = DateTime. now. toString ("yyyyMMddHHmmss_ffff"); sbui. append ("Error level: \ r \ n"); sbui. append (ErrorType); sbui. append ("\ r \ n error message: \ r \ n"); sbui. append (ex. message); sbui. append ("\ r \ n wrong Stack: \ r \ n"); sbui. append (ex. stackTrace); sbui. append ("\ r \ n error method name: \ r \ n"); sbui. append (ex. targetSite. name); sbui. append ("\ r \ n error Class Name: \ r \ n"); sbui. append (ex. targetSite. declaringType. fullName); WriteErrorLog (sbui. toString ();} // obtain the value of the session. You can obtain the object public static object GetSession (string name) {Object obj_Value = null; try {obj_Value = HttpContext. current. session [name];} catch (Exception ex) {obj_Value = null; WriteErrorLog (ex, "HelperError");} return obj_Value ;} // This string is public static string GetStringSession (string name) {string Str_Value = ""; try {if (HttpContext. current. session [name]! = Null) Str_Value = HttpContext. current. session [name]. toString ();} catch (Exception ex) {Str_Value = String. empty; WriteErrorLog (ex, "HelperError");} return Str_Value ;}

 

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.