The usage of session in C # is introduced in detail

Time:2021-1-24

Introduction to session model

Before learning, we will wonder, what is session? In short, it is a number given by the server to the client. When a WWW server is running, there may be several users browsing the website running on the server. When each user establishes a connection with the WWW server for the first time, he establishes a session with the server, and the server will automatically assign a session ID to identify the unique identity of the user. This session ID is a 24 character string randomly generated by the WWW server. We will see it in the following experiment.

This unique session ID is of great practical significance. When a user submits a form, the browser will automatically attach the session ID of the user to the HTTP header information (this is the automatic function of the browser, and the user will not be aware of it). After the server processes the form, it will return the result to the user corresponding to the session ID. Imagine if there is no session ID, how can the server know which user submitted which form when two users register at the same time. Of course, sessionid has many other functions, which we will discuss later.

In addition to the session ID, there is a lot of other information in each session. But for ASP or ASP.NET The most useful program is to visit ASP/ ASP.NET The built-in session object for each user stores their own information. For example, we want to know that the users visiting our website visited several pages, and we may add them to each page that the users may visit. Now that we have a certain understanding of sassion, let’s introduce the properties and features of sassion.

Session has the following characteristics

(1) The data in session is saved in the server
(2) Any type of data can be saved in session
(2) The default session life cycle is 20 minutes, and you can manually set a longer or shorter time.

It should be noted that storing too much data in session variable will consume more server resources, so you should be careful when using session.

Specific usage of session

(1) Save string:

Session["userName"] = "aaa";

(2) Value:

string str = Session["userName"].ToString();

When obtaining a session, it is usually written as follows: String username = session [“username”], but when assigning a value to an object, we need to pay attention to two points:

2.1: judge whether it is null

2.2: type conversion

For example, the number of a record:
    Session[“id”] = 1;
The value is as follows:
    int id1 = Convert.ToInt32(Session[“userName”]);

(3) Release method: clear a session

     Session[“UserName”] = null;
     Session.Remove(“UserName”);

Clear all sessions

     Session.Abandon();

     session.removeAll();

Location and form of session data storage

web.config Configuration node syntax:

Mode: set where session information is stored

Off: no session function is used;

InProc: store session in IIS process, this isDefault valueAnd the mostCommonly used(the advantages are simplicity and highest performance. However, the session is lost when the IIS server is restarted.) ;

Stateserver: store session in ASP.NET State service process (retains session state when restarting a web application and makes session state available to multiple web servers in the network.) ;

SQL Server: the session is stored in SQL Server (it is stored in memory and disk, and it will still exist after the server is hung up and restarted).

Cookieless: sets where the session information of the client is stored

In this case, the session information of the client is no longer stored by cookie, but by URL.

False uses cookie mode, which isDefault value

Timeout sets how many minutes the server will automatically give up the session information. The default is 20 minutes.

Stateconnectionstring sets the server name and port number used to store session information in the state service

For example: “TCPIP = 127.0.0.1:424”. This property is required when the value of mode is stateserver. (default port 424).

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 sets the number of seconds after which the TCP / IP connection between the web server and the server storing the status information is disconnected when stateserver mode is used to store the session status.The default is 10 seconds.

Recommended Today

Interface control design of NAND flash

NAND flash is one of the flash memories. NAND flash adopts nonlinear macro cell mode and provides a cheap and effective solution for the realization of solid-state mass memory.NAND FLASHMemory has the advantages of large capacity and fast rewriting speed, which is suitable for the storage of large amounts of data, so it has been […]