Modes of Session State Management

By AI:

Here are the 5 modes to store Session State in ASP.NET, each with its own storage location and behavior. This is especially useful for configuring ASP.NET (Framework) Web Forms or MVC applications — though some modes also apply to ASP.NET Core with slight differences.

You can copy and share this content freely on your blog or Facebook. ✅


🔐 5 Session State Modes in ASP.NET

Session state helps store user-specific data (like login info, preferences, shopping cart) across multiple requests and pages. ASP.NET provides five storage modes for session state, configured in web.config.


1️⃣ InProc (In-Process)

  • Default mode: Session data is stored in memory on the web server.
  • Fastest option because it doesn’t involve external storage.
<sessionState mode="InProc" />

Pros:

  • Fastest
  • Easy to implement

Cons:

  • Data is lost if the server restarts
  • Not suitable for web farms (multi-server setups)

2️⃣ StateServer (Out-of-Process)

  • Stores session data in a separate ASP.NET State Service (runs as a Windows service).
  • Can be on the same machine or another server.
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" />

Pros:

  • More durable than InProc
  • Supports web farms (multiple servers)

Cons:

  • Slower than InProc (uses serialization and networking)
  • Requires running the state service

3️⃣ SQLServer

  • Stores session data in a SQL Server database.
  • Great for durability and scalability.
<sessionState mode="SQLServer" sqlConnectionString="data source=YourDBServer;user id=sa;password=pass" />

Pros:

  • Highly durable (survives server restarts and crashes)
  • Works well in web farms

Cons:

  • Slower due to database access
  • Requires database setup and maintenance

4️⃣ Custom

  • You can define your own custom session state provider (e.g., Redis, MongoDB, distributed cache).
<sessionState mode="Custom" customProvider="MyProvider">
  <providers>
    <add name="MyProvider" type="YourNamespace.YourCustomProvider" />
  </providers>
</sessionState>

Pros:

  • Flexible
  • Can integrate modern distributed caching solutions

Cons:

  • Requires more development work
  • Performance and reliability depend on your implementation

5️⃣ Off

  • Disables session state entirely.
<sessionState mode="Off" />

Use case: For static content or pages that don’t need session state (improves performance).


🔁 Summary Table

ModeStored InSpeedSupports Web FarmSurvives App RestartNotes
InProcServer Memory✅ Fastest❌ No❌ NoDefault, simple apps
StateServerWindows Service⚠ Medium✅ Yes✅ YesNeeds external service
SQLServerSQL Database⚠ Slower✅ Yes✅ YesGood for large-scale apps
CustomYour choice⚠ Varies✅ Yes✅ DependsFor custom cache/storage
Off✅ N/A✅ Yes✅ YesWhen session isn’t needed

🔧 How to Set Session Mode in web.config

<configuration>
  <system.web>
    <sessionState mode="InProc" timeout="20" />
  </system.web>
</configuration>

Let me know if you want a code example, diagram, or PDF cheat sheet for these session modes!