Implement Cookie set and cookie set

Source: Internet
Author: User

Implement Cookie set and cookie set

In the past, Insus. NET had written a blog titled "using Cookie sets in programs" http://www.cnblogs.com/insus/archive/2011/05/25/2055531.html.

Yesterday, Insus. NET was named in a special project using this method for rewriting and optimization.

Extract and separate Cookie classes.

 

Private static HttpResponse Response {get {return HttpContext. Current. Response ;}} private static HttpRequest Request {get {return HttpContext. Current. Request ;}}Source Code

 

This cookie set, of course, can be accessed only when the individual cookie is:

 

Public static string GetCookie (string cookieName) {return Request. Cookies [cookieName] = null? "": Request. cookies [cookieName]. value;} public static void SetCookie (string cookieName, string value) {Response. cookies [cookieName]. value = value;} public static void RemoveCookie (string cookieName) {HttpCookie cookie = Request. cookies [cookieName]; Cookies. expires = DateTime. now. addTicks (-1); Response. cookies. add (cookie );}Source Code

 

The following is a set of stored cookies, that is, multiple key values can be stored in a single cookie:



Private static Dictionary <string, object> GetCookies (string cookieName) {Dictionary <string, object> dic = new Dictionary <string, object> (); if (Request. cookies [cookieName]! = Null) {HttpCookie cookie = Request. Cookies [cookieName]; if (cookie! = Null) {NameValueCollection nvc = cookie. values; foreach (string key in nvc. allKeys) {dic. add (key, cookie [key]) ;}} return dic ;}public static object GetCookies (string cookieName, string keyName) {Dictionary <string, object> dic = GetCookies (cookieName); if (dic. count> 0 & dic. containsKey (keyName) {return dic [keyName];} return null;} public static void SetCookies (string cookieName, Dic Tionary <string, object> dic) {HttpCookie cookie = Request. Cookies [cookieName] = null? New HttpCookie (cookieName): Request. cookies [cookieName]; foreach (KeyValuePair <string, object> val in dic) {cookie [val. key] = val. value. toString ();} Response. cookies. add (cookie);} public static void RemoveCookies (string cookieName) {HttpCookie cookie = Request. cookies [cookieName]; Cookies. expires = DateTime. now. addTicks (-1); Response. appendCookie (cookie);} public static void RemoveCookies (str Ing cookieName, string keyName) {HttpCookie cookie = Request. Cookies [cookieName]; if (cookie! = Null) cookie. Values. Remove (keyName); Response. AppendCookie (cookie );}Source Code



The above is just a cookie class. How do we write cookies in programs? That is, how to use it. You can write every cookie as a property as follows:



You can write any cookie you want to implement. The above is just a simple method to write two attributes.

In the web page, you can easily use:
Assignment:
InsusBase. SystemLoginOk = true;
InsusBase. SystemId = "Key001 ";


Get value:
Response. Write (InsusBase. SystemLoginOk );
Response. Write (InsusBase. SystemId );

 

 



 

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.