Session state with Azure Redis cache in Azure App Service

This topic explains how to use the Azure Redis Cache Service for session state.

If your ASP.NET web app uses session state, you will need to configure an external session state provider (either the Redis Cache Service or a SQL Server session state provider). If you use session state, and don't use an external provider, you will be limited to one instance of your web app. The Redis Cache Service is the fastest and simplest to enable.


Although this article refers to web apps, it also applies to API apps and mobile apps.

Create the Cache

Follow these directions to create the cache.

Add the RedisSessionStateProvider NuGet package to your web app

Install the NuGet RedisSessionStateProvider package. Use the following command to install from the package manager console (Tools > NuGet Package Manager > Package Manager Console):

PM> Install-Package Microsoft.Web.RedisSessionStateProvider

To install from Tools > NuGet Package Manager > Manage NugGet Packages for Solution, search for RedisSessionStateProvider.

For more information see the NuGet RedisSessionStateProvider page and Configure the cache client.

Modify the Web.Config File

In addition to making assembly references for Cache, the NuGet package adds stub entries in the web.config file.

  1. Open the web.config and find the the sessionState element.
  2. Enter the values for host, accessKey, port (the SSL port should be 6380), and set SSL to true. These values can be obtained from the Azure Portal blade for your cache instance. For more information, see Connect to the cache. Note that the non-SSL port is disabled by default for new caches. For more information about enabling the non-SSL port, see the Access Ports section in the Configure a cache in Azure Redis Cache topic. The following markup shows the changes to the web.config file, specifically the changes to port, host, accessKey, and *ssl.

         <customErrors mode="Off" />;
         <authentication mode="None" />;
         <compilation debug="true" targetFramework="4.5" />;
         <httpRuntime targetFramework="4.5" />;
         <sessionState mode="Custom" customProvider="RedisSessionProvider">;
               <!--<add name="RedisSessionProvider" 
                 host = "" [String]
                 port = "" [number]
                 accessKey = "" [String]
                 ssl = "false" [true|false]
                 throwOnError = "true" [true|false]
                 retryTimeoutInMilliseconds = "0" [number]
                 databaseId = "0" [number]
                 applicationName = "" [String]
              <add name="RedisSessionProvider" 
                   ssl="true" />;
           <!--<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="" accessKey="" ssl="false" />;-->;

Use the Session Object in Code

The final step is to begin using the Session object in your ASP.NET code. You add objects to session state by using the Session.Add method. This method uses key-value pairs to store items in the session state cache.

string strValue = "yourvalue";
Session.Add("yourkey", strValue);

The following code retrieves this value from session state.

object objValue = Session["yourkey"];
if (objValue != null)
   strValue = (string)objValue;    

You can also use the Redis Cache to cache objects in your web app. For more info, see MVC movie app with Azure Redis Cache in 15 minutes. For more details about how to use ASP.NET session state, see ASP.NET Session State Overview.


If you want to get started with Azure App Service before signing up for an Azure account, go to Try App Service, where you can immediately create a short-lived starter web app in App Service. No credit cards required; no commitments.

What's changed