ASP. MVC5 Summary (iii) session and cookie of common technical analysis in landing

Source: Internet
Author: User

1.session mechanism

Session mechanism is on the server side to maintain the state of the scheme, in the system landing, we often use the session to store some user login important information, and this information can not exist in the cookie.

When the amount of traffic increases, the server performance is compared.

How to write a session

1  Public Static void Writesession (string  key, T value)2{3if  (key). IsEmpty ())4return; 5 Httpcontext.current.session[key] = value; 6 }
View Code

How to read the session

 1  public  static  string  GetSession (string   key)  2  3  if   (key. IsEmpty ())  4  return  string  . Empty;  5  as  string   6 } 
View Code

method to delete the specified session

1  Public Static void Removesession (string  key)2        {3             if  (key). IsEmpty ())4                 return; 5             HttpContext.Current.Session.Contents.Remove (key); 6         }
View Code

2.cookie mechanism

The cookie mechanism adopts the scheme of keeping state on the client, when we make the system landing, we will store the basic information of the user to the user's browser, we will use the cookie, but it can save 4K data.

Ways to write cookies

1  Public Static voidWritecookie (stringStrName,stringstrvalue)2         {3HttpCookie cookie =Httpcontext.current.request.cookies[strname];4             if(Cookie = =NULL)5             {6Cookie =NewHttpCookie (strName);7             }8Cookies. Value =strvalue;9 HttpContext.Current.Response.AppendCookie (cookie);Ten}
View Code

Ways to read cookies

1  Public Static stringGetCookie (stringstrName)2         {3             if(HttpContext.Current.Request.Cookies! =NULL&& Httpcontext.current.request.cookies[strname]! =NULL)4             {5                 returnHttpcontext.current.request.cookies[strname]. Value.tostring ();6             }7             return "";8}
View Code

Ways to delete cookies

1  Public Static void Removecookie (string  cookiesname)2        {3             new  HttpCookie (Cookiesname.trim ()); 4             Objcookie.expires = DateTime.Now.AddYears (-5); 5             HttpContext.Current.Response.Cookies.Add (Objcookie); 6         }
View Code

ASP. MVC5 Summary (iii) session and cookie of common technical analysis in landing

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.