ASP.NET Session State: Storing Data Beyond IIS Restarts

To keep the user session alive even after an IIS restart in ASP.NET, you need to store session data outside of the default in-memory session state.

1. Use SQL Server Session State

The best approach to persist session data even after an IIS reset is to store it in an SQL Server database.

Steps to Configure SQL Server Session State

1. Enable SQL Server session state in Web.config

<configuration>
    <system.web>
        <sessionState mode="SQLServer"
                      sqlConnectionString="Data Source=your_server;Initial Catalog=ASPState;Integrated Security=True"
                      cookieless="false"
                      timeout="60" />
    </system.web>
</configuration>

2. Run the SQL Server Session State Setup Script

EXEC aspnet_regsql.exe -S YourSQLServer -E -ssadd -sstype p

The session will persist even after an IIS restart.

2. Use State Server Mode (Out-of-Process Session)

If you prefer not to use SQL Server, you can configure a separate session state service.

Steps to Configure

1. Enable State Server in Web.config

<configuration>
    <system.web>
        <sessionState 
            mode="StateServer" 
            stateConnectionString="tcpip=127.0.0.1:42424" 
            cookieless="false" 
            timeout="60" />
    </system.web>
</configuration>

2. Start ASP.NET State Service

Open Run (Win + R) → Type services.msc → Find ASP.NET State Service → Start it.

The session will persist after an IIS restart but will be lost if the State Server service is stopped.

3. Use a Distributed Cache (Redis)

If you have multiple servers, using Redis for session storage is an excellent option.

Steps to Configure Redis Session State

1. Install Redis Session Provider via NuGet

    Install-Package Microsoft.Web.RedisSessionStateProvider

2. Update Web.config

<configuration>
    <system.web>
        <sessionState mode="Custom" customProvider="RedisSessionProvider">
            <providers>
                <add name="RedisSessionProvider"
                     type="Microsoft.Web.Redis.RedisSessionStateProvider"
                     host="your_redis_server"
                     port="6379"
                     accessKey="your_redis_key"
                     ssl="false"/>
            </providers>
        </sessionState>
    </system.web>
</configuration>

Sessions will persist even if IIS restarts and it works well in cloud environments.

Which Method Should You Use?
 

Method Session Persistence Performance Best For
SQL Server ✅ Survives IIS Restart ⚡ Medium Multiple IIS servers
State Server ✅ Survives IIS Restart ⚡ Faster than SQL Single-server applications
Redis ✅ Survives IIS Restart 🚀 Fastest Cloud & distributed apps


Final Recommendation

  • If you are running a single-server app, use State Server.
  • If you are using multiple IIS servers, go with SQL Server Session State.
  • If you need a high-performance, scalable solution, use Redis.

Up Next
    Ebook Download
    View all
    Learn
    View all