- Introduction
- Configuring In-Process Session State
- Configuring Out-of-Process Session State
- Disabling or Enabling Session State at the Page Level
- Working with Cookieless Session-State Management
- Summary
Configuring In-Process Session State
By default, session state is configured to be stored in-process. The process we're referring to is the aspnet_wp.exe worker process. In-process session management is fast and convenient, is appropriate for small to moderately sized amounts of data, and is configured by default in the machine.config file and your application's Web.config file. Listing 1 demonstrates how to place simple data in the session cache, and Listing 2 provides an overview of the session configuration elements in the Web.config file.
Listing 1Adding Simple Data to a Page's Session Cache
private void Button1_Click(object sender, System.EventArgs e) { Session["Value"] = TextBox1.Text; }
The code in Listing 1 presumes that we're in an ASP.NET web application, and a single web page contains TextBox and Button controls. When the Button click event fires, the value of the TextBox's Text property is stored in the session cache. You can understand the code to be referring to the this.Page.Session property.
Listing 2The Default Session Configuration from an ASP.NET Web Application
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" />
Listing 2 is an excerpt from a Web.config filespecifically, from the Web.config file for the web application referenced in Listing 1. The sessionState tag is used to describe how session information is stored. Various sessionState attributes come into play for different state servers.
CAUTION
The Help documentation shows the mode for in-process state management to be incorrectly capitalized as Inproc. You'll get a configuration error at runtime if the mode value's case is incorrect.
The case-sensitive values for the mode attribute can be one of the following: InProc, Off, StateServer, or SQLServer:
InProc means that Session information will be stored in memory in the aspnet_wp.exe worker process.
Off indicates that Session is disabled.
StateServer indicates that the out-of-process aspnet_state.exe state server will be used to manage session state.
SQLServer indicates that Microsoft SQL Server will be used to manage session state.
Both the StateServer and SQLServer modes require some additional configuration.
The stateConnectionString attribute is used by the aspnet_state.exe session state server. This attribute indicates the IP address and port of the state server. By default, the stateConnectionString refers to the same machine that Web.config is on. You can cache session state remotely by pointing to the IP address of another machine.
The sqlConnectionString is a connection string for a Microsoft SQL Server instance configured to manage session state information.
The last two values of the sessionState tag indicate whether cookies are used to store the session ID (cookieless) and the session cache expiration time (timeout). By default, cookies are used to store the session ID, and entries in the cache expire after 20 minutes. You can change the cookieless attribute to true for devices and browsers that don't support cookies, and the session ID will be embedded as part of the URL.