About Session,cookie,cache

Source: Internet
Author: User

Yesterday, after reading the "several ways to transfer values between ASP.", there is a closer look at Session,cookie,cache, and here's what's relevant

First, Session 1. Session Basic operation

A. Create session

Create session public        void CreateSession (string[] arrstr)        {            //create array            string[] str=new string[ Arrstr.length];            for (int i = 0; i < arrstr.length; i++)            {                Str[i] = i.tostring ();                Session[str[i]] = Arrstr[i];            }        }

b, get the value of session

String  getsessionvalue=session["name"]. ToString ();

C, Traverse session

Traverse Session public        void GetSession ()        {            IEnumerator sessionenum = Session.Keys.GetEnumerator ();            while (Sessionenum.movenext ())            {                Response.Write (session[sessionenum.current.tostring ()). ToString () + ";");            }        }

D. Clear session, but not end

Clears the session, but does not end the conversation public        void Clearsession ()        {            session.clear ();        }

E. End Session

End session public        void Abandonsession ()        {            Session.Abandon ();        }
2. Session data storage form and location
<system.web>  <sessionstate mode= "off| inproc| stateserver| SQL Server "  cookieless=" True|false "  timeout=" number of minutes "  stateconnectionstring=" tcpip= Server:port "  sqlconnectionstring=" SQL connection string "  statenetworktimeout=" number of seconds "/> </system.web>

Annotations:

Mode: Indicates setting storage session form and location;

A, off: Disable session;

b, inproc:in process abbreviation, indicating that the session is stored in the IIS process, but note that although the performance is high, but IIS restart is, lost session information; (default value)

C, Sateserver: Store the session in the ASP. NET State Service process (the session state is preserved when the Web application is restarted and the session state can be used for multiple Web servers in the network);

D. Store the session in SQL Server

Cookieless: Setting client storage session form and location

A, true: using the cookieless mode, the client session information is no longer using cookies stored, but it is stored through the URL;

B, false: Use Kookie mode, default value.

Timeout sets the number of minutes after which the server automatically discards session information. The default is 20 minutes;

stateConnectionString sets the server name and port number used when the session information is stored in the State service, for example: "tcpip=127.0.0.1:42424". When the value of mode is StateServer Yes, this property is required. (default port 42424);

sqlConnectionString sets the connection string when connecting to SQL Server. For example, "Data source=localhost;integrated security=sspi;initial Catalog=joye". This property is required when the value of mode is SQL Server;

stateNetworkTimeout settings when the session state is stored using StateServer mode, the TCP/IP connection to the server that stores the state information is disconnected after the number of seconds that the Web server is idle. The default value is 10 seconds;

3. Session principle

Why was the session introduced? As you know, because HTTP is a stateless protocol, the session is making up for this flaw. Of course, the role of the session far more than these, here is not much discussion.

Session in ASP., the client (Goggle,firefox,ie, etc.) and server-side sessions are used to store specific session information and, to be precise, to store specific user information. When the client sends a request to the server, such as the login user ID, the server receives the request, the server side session generates a SessionID associated with the logged-in user, and returns the Sessioid to the client (Goggle,firefox,ie, etc.). At the beginning of a new session, the server stores SessionID as a cookie in the user's browser.

Summarize:

1. Definition: System.Web.SessionState.HttpSessionState page.session//Gets the current Session object provided by ASP.

2. Characteristics:

A, the session Chinese means "session", in ASP. NET, represents a session between the client and server, one of the common sessions in the Web.

B. The session is stored in server-side memory.

C, session can store any type of data, including custom objects.

D, session and session are independent of each other, non-interference.

E, session and cookie pairing use, session on the server side generates SessionID, and return the SessionID to the client (Ie,firefox,google, etc.), the client cookie to store the SessionID,

As long as the SessionID cookie is not lost, the session information will not be lost during the whole conversation.

F, the session saved data can be accessed across pages, that is, the cross-page is global.

G, session cannot be accessed across processes, only by users of that session.

H, the session information can be cleared without ending the conversation, that is, call Session.clear ();

I, when the session ends, expires, the server clears the session object.

J, session is often used to save the ID of the logged-on user.

Second, Cache

Core code:

CLASS1 cache["id"] = TextBox1.Text; Response.Redirect ("~/webform1.aspx");//class2if (cache["id"]!=null)  {     Label1.Text = cache["id"]. ToString ();  } Remove the cache Cache.remove ("id");//if cache["id"] is empty, the pass value fails. The following method can be used for the actual//period of 10 minutes Cache.Insert ("id", Textbox1.text,null,cache.noabsoluteexpiration,new TimeSpan (0,10,0));

Summarize:

1. The caching mechanism in the application is used to store objects created with a large number of server resources in memory, which greatly improves the performance of the application. This mechanism can also be used to transmit values.

2, unlike other methods, the method needs to set the cache item priority and cache time. Because when system memory is scarce, the caching mechanism automatically removes items that are seldom used or lower in priority, resulting in a failed pass.

3, the advantages of this method is the transmission of data size and quantity of unrestricted, fast. The disadvantage is that the operation of the caching mechanism is relatively complex.

Third, cookies

Core code:

Class1httpcookie HttpCookie = new HttpCookie ("TestCookie", "page transfers by Cookie"); Response.Redirect ("~/class2.aspx");//class2label1.text = request.cookies["TestCookie"]. Value;

Summarize:

1, the cookie is used to store small pieces of information in the user's browser, to save the user's relevant information, such as the user's access to a website user ID, user preferences, etc., the next visit by the user can be retrieved

Get the previous information. So cookies can also pass values between pages.

2. The cookie is passed back and forth between the browser and the server via the HTTP header. A cookie can contain only the value of a string, and if you want to store an integer value in a cookie, you need to first convert to a string.

3, as with the session, it is what for each user, but there is an essential difference, that is, the cookie is stored in the client, and the session is stored on the server side. And the cookie makes

Use the request to work with the ASP. NET built-in object.

4, easy to use, is to maintain the user state of a very common method. For example, in a shopping site, users can use it to maintain user status when they cross multiple page forms.

5, is often considered to collect user privacy and has been criticized.

6, security is not high, easy to forge.

About Session,cookie,cache

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.