Session Management Techniques

ASP.NET provides three modes of session state storage controlled by mode attribute of <sessionState> tag in your web application’s Web.config file. Below is a sample of this tag:

sqlConnectionString=”data source=;Trusted_Connection=yes”

There are three different modes.
Session state is stored locally in memory of ASP.NET worker process.
Session state is stored outside ASP.NET worker process and is managed by Windows service. Location of this service is specified by stateConnectionString attribute.
Session state is stored outside ASP.NET worker process in SQL Server database. Location of this database is represented by sqlConnectionString attribute.

Session State Configuration and Startup

Configuring In-process Mode
In-process is the default session state mode. To use in-process mode, set the mode attribute of the element to Inproc.

Configuring State Server Mode
To use State Server, you must first make sure ASP.NET state service is running on the remote server used for the session store. This service is installed with ASP.NET and Visual Studio .NET at the following location:
Next, set the mode attribute of the element to StateServer in the Web.config file of the application. Finally, set the connectionString attribute to tcpip=serverName:portNumber.

Configuring SQL Server Mode
To use SQL Server, first run either InstallSqlState.sql or InstallPersistSqlState.sql on the computer with SQL Server that will store the session state. Both scripts create a database called ASPState that includes several stored procedures. The difference between the scripts is where the ASPStateTempApplications and ASPStateTempSessions tables are placed. The InstallSqlState.sql script adds these tables to the TempDB database, which loses session data if the computer is restarted. The InstallPersistSqlState.sql script, on the other hand, adds these tables to the ASPState database, which allows session data to be retained when the computer is restarted.
Both of these script files are installed by default at the following location:
Next, set the mode attribute of the element to SQLServer in the Web.config file of the application. Finally, set the sqlConnectionString attribute to Integrated Security=SSPI;data source=serverName;.

In SQL Server mode, session state can also be configured to work in a failover cluster. A failover cluster is two or more identical, redundant Web servers that store their session data on a separate computer in a SQL Server database. If a Web server fails, another server in the cluster can take over and serve requests without session data loss. To configure a failover cluster, set the <MachineKey> element in the Web.config file of the Web servers to the same value. Then set the SQL connection string of the Web servers to point to the SQL Server database on the computer that stores the session data.

Performance and Reliability Considerations

It’s worth mentioning, briefly, some of the performance and reliability issues you should consider when using ASP.NET session state modes.

In process. In process will perform best because the session state memory is kept within the ASP.NET process. For Web applications hosted on a single server, applications in which the user is guaranteed to be re-directed to the correct server, or when session state data is not critical (in the sense that it can be re-constructed or re-populated), this is the mode to choose.

Out of process. This mode is best used when performance is important but you can’t guarantee which server a user will request an application from. With out-of-process mode, you get the performance of reading from memory and the reliability of a separate process that manages the state for all servers.

SQL Server. This mode is best used when the reliability of the data is fundamental to the stability of the application, as the database can be clustered for failure scenarios. The performance isn’t as fast as out of process, but the tradeoff is the higher level of reliability.

I hope you like this article. I have taken this article from microsoft website.

Posted in Microsoft Technology Tagged with:
2 comments on “Session Management Techniques
  1. Shilpa Ginode says:


    This article is very good. Thanks for it.
    It is simple way of explaning the session management i have ever seen.
    Keep it up..

    Shilpa Ginode
    L&T Infotech

  2. Bala says:

    Nice Article. Thanks for sharing

1 Pings/Trackbacks for "Session Management Techniques"
  1. […] Session Management Techniques […]