Simple Cookie usage and simple Cookie usage
Cookies are familiar to everyone. They are usually used in many places. Now I will briefly introduce their methods for adding, deleting, modifying, and querying cookies, of course, the core is to add a simple graph to illustrate the two methods of adding cookies.
Now let's start to explain the above diagram.
Step 1: Add a Cookie
You can add a Cookie in either of the following ways. In fact, we can regard cookies as an object.
1: Add without subkeys
We first give the Cookie we created a name, and then assign a value to this Cookie. Of course, we can set other Cookie attributes to indicate that the expiration time is the most common one. Finally, we add the response, in this way, a Cookie is added.
2: add a sub-Key
Similarly, you need to give the new Cookie a name, then add a subkey to the Cookie object, and assign a value
# Add a Cookie to region /// <summary> /// set a cookie value /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value </param> /// <param name = "duration"> duration, the default unit is 4 hours </param> public static void SetCookie (string cookieName, string key, string value, double duration = 4.0) {if (HttpContext. current = null | string. isNullOrEmpty (key) | string. isNullOrEmpty (value) return; var cookie = new HttpCoo Kie (cookieName); // create and name a new Cookie. Cookie. Expires = DateTime. Now. AddHours (4.0); // set the expiration time if (! String. isNullOrEmpty (key) {cookie. values. add (key, value); // sub-key} else {cookie. value = value; // No subkey} HttpContext. current. response. cookies. add (cookie) ;}# endregion
Step 2: delete a Cookie
Deleting a Cookie also determines whether a child key exists. If a child key exists, the Child key is deleted. If no child key exists, the Cookie is removed.
# Region Delete Cookie // <summary> // Delete Cookie // </summary> // <param name = "cookieName"> Cookie name </param>/ // <param name = "key"> Cookie subkey </param> public static void Delete (string cookieName, string key) {HttpResponse response = HttpContext. current. response; if (response! = Null) {HttpCookie cookie = response. Cookies [cookieName]; if (cookie! = Null) {if (! String. IsNullOrEmpty (key) & cookie. HasKeys) {cookie. Values. Remove (key) ;}else {response. Cookies. Remove (cookieName) ;}}# endregion
Step 3: Obtain the Cookie
You can use the Cookie name, Cookie name, and sub-Key to obtain the Cookie. See the following code:
# Region obtain Cookie // <summary> // obtain the Cookie value based on the Cookie name and subkey (the subkey can be blank) /// </summary> /// <param name = "CookieName"> Cookie name </param> /// <param name = "Key"> Cookie subkey </ param> // <returns> </returns> public static string GetValue (string cookieName, string key) {if (string. isNullOrEmpty (cookieName) | HttpContext. current. request = null) {return "";} if (HttpContext. current. request. cookies [cookieName]! = Null) {if (! String. isNullOrEmpty (key) & HttpContext. current. request. cookies [cookieName]. hasKeys) {return HttpContext. current. request. cookies [cookieName]. values [key];} else {return HttpContext. current. request. cookies [cookieName]. value ;}} return "" ;}# endregion