ASP.NET Session object holds session instructions

  • 2020-05-19 04:29:46
  • OfStack

ASP.NET provides Session objects that allow programmers to identify, store, and process contextual information about several requests from the same browser object to a particular network application on the server. Session corresponds to the same conversation between the browser and the server. When the browser requests a page of the network application in the first place, the server will trigger the Session_onStart event. The Session_onEnd event is triggered when a conversation times out or is closed. The programmer can respond to these two events in the code to handle tasks related to the same conversation, such as creating and releasing resources to be used for that conversation.

To use the Session object in the ASP.NET program, you must make sure that the EnableSessionState property in the @page directive on the page is True or Readonly, and that the SessionState property is properly set in the web.config file.
The state of Session in ASP.NET is maintained by the web.config file < system.web > Under the flag of < sessionstate > The mode attribute of the tag is determined. There are four possible values for this property: Off, Inproc, StateServer, and SQlServer.

Setting it to Off disables Session.
Inproc is the default setting. This mode is similar to the previous ASP session-state method, where the session-state is saved in the ASP.NET process. The advantage is obvious: performance. Data access within a process is naturally faster than that of the kua process. However, the state of this method, Session, depends on the ASP.NET process, and when the IIS process crashes or restarts normally, the state saved in the process is lost.
To overcome the disadvantages of the Inproc pattern, ASP.NET provides two ways to maintain session state outside of process.
ASP.NET first provides an Windows service: ASPState. When the service is started, the ASP.NET application can set the mode property to "SateServer" to use the state management method provided by the Windows service.
In addition to setting the mode property to StateServer in the web.config file, you must also set the IP address and port number to run the StateServer server.

mode StateServer = ""
stateConnectionString="tcpip=127.0.0.1:42424"
With this pattern, the storage of session state is not dependent on the failure or restart of the IIS process, and the state of the session is stored in the memory space of the StateServer process.

The other session-state mode is SQLServer mode. This mode stores the state of the session in the SQLServer database. Before you can use this pattern, you must have at least one SQLServer server and set up the required tables and stored procedures on the server. .NETSDK provides two scripts to simplify the task: InstallSqlState.sql and UnInstallSqlState.sql. The documents of the two countries are stored in the following paths:
< %SYSTEMDRIVER% > \Winnt\Microsoft.NET\Framework\ < %version% > \
To configure the SQLServer server, run osql.exe, the command-line tool provided by SQLServer, from the command line
osql-s[servername]-u[user]-p[password] < InstallSqlState.sql

Such as:
osql - s (local) - uas - p "" - iInstallSqlState. sql
After doing the necessary database preparation, change the mode attribute of the sessionstate element in the web.config file to "sqlserver" and specify the SQL connection string. The details are as follows:

mode="SQLServer"
sqlConnectionString="datasource=127.0.0.1;userid=sa;password=;Trusted_Connection=yes"
Using SQLServer mode makes the state of Session independent of the IIS server, and it can also take advantage of the cluster of SQLServer to make the state store independent of a single SQLServer, thus providing great reliability for the application.

Summary: seesion maintains sessions in two ways, and cookie/ stores data

Related articles: