Introduction to Session Model
Before studying, we will wonder, what is Session? Simply put, it is the number given to the client by the server. When a WWW server is running, several users may browse the website running on the server. When each user establishes a connection with this WWW server for the first time, he establishes a session with the server, and the server will automatically assign it a SessionID to identify the user's unique identity. This SessionID is a 24-character string randomly generated by the WWW server. We will see what it actually looks like in the experiment below.
This unique SessionID has great practical significance. When a user submits a form, the browser will automatically append the user's SessionID to the HTTP header information (this is the automatic function of the browser and the user will not notice it). When the server processes the form, the result will be returned to the user corresponding to the SessionID. Just imagine, if there is no SessionID, when two users register at the same time, how can the server know which user submitted which form? Of course, SessionID has many other functions, which we will mention later.
In addition to SessionID, there is a lot of other information included in each Session. However, for writing ASP or programs, the most useful thing is to store their own information for each user by accessing the ASP/ built-in Session object. For example, we want to know how many pages the users visiting our website have browsed, and we may include them in each page that the users may visit. At this point, we have a certain understanding of Sassion. Let’s introduce the attributes and characteristics of Sassion.
Session has the following characteristics
(1) The data in the Session is stored on the server side;
(2) Any type of data can be saved in the Session;
(2) The default life cycle of Session is 20 minutes, and it can be manually set to a longer or shorter time.
It should be noted that storing too much data in the Session variable will consume more server resources, so you should be cautious when using session.
Specific usage of Session
(1) Save the string:
Session["userName"] = "aaa";
(2) Value:
string str = Session["userName"].ToString();
When obtaining session, it is usually written like this: string username=session["username"], but when assigning values to objects, we need to pay attention to two points:
2.1: Determine whether it is null
2.2: Type conversion
What is the number of a record:
Session["id"] = 1;
This way the value:
int id1 = Convert.ToInt32(Session["userName"]);
(3) Release method: Clear a Session
Session["UserName"] = null;
("UserName");
Clear all Sessions
();
();
The location and form of the session data storage
Configure node syntax:
<> <sessionState mode="Off|InProc|StateServer|SQLServer" cookieless="true|false" timeout="number of minutes" stateConnectionString="tcpip=server:port" sqlConnectionString="sql connection string" stateNetworkTimeout="number of seconds" /> </>
mode: Set where to store Session information
Off: Do not use the Session function;
InProc: Store the Session in the IIS process, which is the default value and is also the most commonly used (the advantage is that it is simple and has the highest performance. However, the Session is lost when restarting the IIS server.);
StateServer: Stores the Session in the State Service process (preserves the session state when restarting the web application and makes the session state available for multiple web servers in the network.);
SQLServer: Store the Session in SQL Server (stored in memory and disk, and the server is still there after it is hung up and restarted).
cookieless: Set where to store the Session information of the client
ture uses the cookieless mode; at this time, the client's Session information is no longer stored using cookies, but is stored through the URL.
false Use cookie mode, which is the default value.
timeout Sets how many minutes the server automatically abandons the Session information. The default is 20 minutes.
stateConnectionString Sets the server name and port number used when storing Session information in the state service
For example: "tcpip=127.0.0.1:42424". This property is required when the value of mode is StateServer. (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 SQLServer.
stateNetworkTimeout Sets how many seconds to disconnect the TCP/IP connection between the web server and the server that stores the status information after the state server is idle. The default value is 10 seconds.
The above is a detailed explanation of the usage of Session in C#. For more information on the usage of Session in C#, please pay attention to my other related articles!