C #, Cookie and Session usage and differences?

C #, Cookie and Session usage and differences?

文献种类:专题技术文献;
开发工具与关键技术:VS
作者:蛋蛋;
撰写时间:2019/05/13

.Cookie stored in the client, by the user's own destruction of client information stored objects, you can put the user information stored in the user's local, do not always have access to the server 2.Session stored on the server, the client (browser) that is close destruction (in the case, and if not using the browser is not closed, destroyed automatically default time of 20 minutes) the user global variables, the operation for all active users are stored in the server 3.Application, no time limit, the server closes that destruction (provided that he did not write the methods of destruction) program global variable object for each user every page valid
application used to store user data common to all the information, if the data will not be saved in the application's lifetime change or very little change with it. But there is a web.config in asp.net, it may be better points. If you want to use the application, a question to consider is any write operation has to be completed (Global.asax) in application_onstart event. Although application.lock and application.unlock way to avoid synchronization operations, but it serializes the request of the application, and when the site Sheremetyevo cause performance bottlenecks. It is best not to use it to access large data sets.
Usage:
// store information
the Application [ "Test"] = "100";
// read
String test = Application [ "test" ] ToString (); Session information is stored in the server's memory, of course, you. it may be provided a method for storing (e.g., the presence of the SQL database). Since the program after the user stops using it for some time remain in memory, so use the Session object to save user data method is inefficient. For small amounts of data. Use Session is a good choice.
Cookie storage capacity is very limited, usually browser supports a maximum capacity of 4096 bytes. Store large amounts of data can not be used. Since not all browsers support Cookie, and it is saved in plain text, so it is best not to save sensitive content. Otherwise it will affect network security session: the object is an object type HttpSession, described a call period between a client and server, in response to a number of times corresponding to the request within the time period comprises a client and a server, the entire time period session object presence. Used to achieve store the current user's shopping cart such data. Different users have their own different session objects.
Session [ "checkcode"] = " "; String checkcode = Session [ "checkcode"]; // save the object Agent agent = new Agent (); Session [ "agent"] = agent; // Value Agent agent = (Agent ) Session [ "agent"]; cookie application HttpCookie cookie = new HttpCookie ( "mycookie "); // cookie object definitions and named item DateTime dt = DateTime.Now of MyCookie; // the defined time target cookie.Values.Add ( "user", "you name "); // increase in property
cookie.Values.Add ( "userid", "123456");
Response.AppendCookie (cookie);
// determine writes a cookie read cookie

Guess you like

Origin blog.csdn.net/qq_42577408/article/details/90246319