Using Azure Redis Cache As A Persistent Storage For ASP.NET Session State

In this post, I will demonstrate how to use Azure Redis Cache for storing ASP.NET session state. In Azure, In-Proc session mode does not work as our application will be working with sticky sessions on a load balancing scenario. In this context, ASP.NET  session state and caching do not work so that we have to put ASP.NET state information on a persistent storage. And it is very important that we should develop the cloud apps in stateless manner so that we can easily scale our apps. In Azure, we can leverage the following storage options as a persistent storage for storing ASP.NET state information.

  • Azure Cache & Azure Redis Cache
  • Azure Table storage
  • SQL Azure

In this post, let’s focus on Azure Redis Cache for storing ASP.NET Session state. Azure Redis Cache is a high performance caching service on the Azure, based on the open source Redis cache. Redis has been widely used as session store by many developer communities due to its simplicity and high performance.

The information below provides how to use Azure Redis Cache for storing ASP.NET session state.

Step 1 – Create Azure Redis Cache

In the Azure portal, create a new Azure Redis Cache as shown in the below figure.


Figure 1 – Creating new Azure Redis Cache

Once you have created the Cache, you can browse the cache services from the Caches section. The figure below shows the summary of Cache in which we can take the properties such as Host Name, Port and SSL Port, and Keys which can be used for connect our client apps with Cache service. Let’s copy the properties and keys of Cache for communicate our client apps with Azure Redis Cache.  


Figure 2 – Summary of the Azure Redis Cache

Step 2 – Configure the ASP.NET App with ASP.NET Session State Provider for Redis

In the previous step, we have created a Cache service on the Azure Redis Cache. Let’s configure our ASP.NET app, in order to use Azure Redis Cache for storing ASP.NET Session values. Microsoft provides an ASP.NET Session state provider for Redis which is available as a NuGet package named RedisSessionStateProvider. You can install this using Package Manager console with the following command.

PM> Install-Package Microsoft.Web.RedisSessionStateProvider –Pre

If you are using Manage NuGet  packages windows, don’t forget to choose the “Include Prerelease” as the this package is currently running in the preview version. When we install the RedisSessionStateProvider package, it will automatically add the following on the web.config file for configure the Azure Redis cache as the storage for ASP.NET Session state provider.

<sessionState mode="Custom" 
          <add name="MySessionStateStore" 
            host = "" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            throwOnError = "true" [true|false]
            retryTimeoutInMilliseconds = "0" [number]
            databaseId = "0" [number]
            applicationName = "" [String]
      <add name="MySessionStateStore" 
         host="" accessKey="" 
          ssl="false" />

From azure portal, let’s copy the properties and access key and put the values on the configuration. Now we can use ASP.NET Session state in a normal manner where we can store and read values from Session, which will be use Azure Redis cache as the storage mechanism for your ASP.NET apps. These apps will be easily scalable on the Azure cloud as the Session state values are storing in a persistent storage.

1 Comment

  • Our problem is that session timeout is not extending with the user's postback, suppose our user logs into the application at 10:00 AM then his session data will expire at 10:20 AM absolute if he does nothing. If user postbacks at 10:15 AM then session should expire at 10:35 AM but this is not happening it is expiring on 10:20 AM absolute. If we set value to any new or existing session variable then session timeout is sliding but if we only fetch session variables then session is timedout at absolute time. Please suggest what may be the issue?

Add a Comment

As it will appear on the website

Not displayed

Your website