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