:::: MENU ::::

ASP.NET session state management with Azure Redis Cache

HTTP is a stateless protocol. To allow users save to state information across requests, ASP.NET provides Session storage. The session variables are stored on per-user basis. In ASP classic, you can store session variables only in the Web server’s memory. However, this approach proves to be poor in terms of scalability and reliability. In later versions of ASP.NET you can customize the session state store as per your requirement. This article will explore one of the scalable and reliable approaches for storing session variables — Azure Redis Cache.

ASP.NET session state supports several different storage options for session data. Each option is identified by a value in the SessionStateMode enumeration. The following list describes the available session state modes:

  • InProc mode, which stores session state in memory on the Web server. This is the default.
  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • Custom mode, which enables you to specify a custom storage provider.
  • Off mode, which disables session state.

You can specify which mode you want ASP.NET session state to use by assigning a SessionStateMode enumeration values to the mode attribute of the sessionState element in your application’s Web.config file. Modes other than InProc and Off require additional parameters, such as connection-string values as discussed later in this topic. You can view the currently selected session state by accessing the value of the HttpSessionState.Mode property.

So Azure Redis Cache falls under custom mode. Let’s talk a bit about custom mode.

Custom mode specifies that you want to store session state data using a custom session state store provider. When you configure your ASP.NET application with a Mode of Custom, you must specify the type of the session state store provider using the providers sub-element of the sessionState configuration element. You specify the provider type using an add sub-element and include both a type attribute that specifies the provider’s type name and a name attribute that specifies the provider instance name. The name of the provider instance is then supplied to the customProvider attribute of the sessionState element to configure ASP.NET session state to use that provider instance for storing and retrieving session data.

That’s what custom mode in Nutshell.

And about Redis, it is an open source key value data structure store. keys can be strings, hashes, lists, sets, sorted sets etc. This in memory data store is broadly used in session state storing and caching.

Let’s start to connect these pieces.

  • Create Instance of Redis Cache from azure portal

  • There are lots of features/settings that you can configure on Redis Cache resource take a look.
  • Now let’s add the RedisSessionStateProvider Nuget package to our web app. Go to Nuget Package Manager console and enter this command

Once these binary added then it will modify(automatically) your web.config file by adding custom session mode information. If you have already other type of session configured then you need to delete those and start to configure new session mode. Here is how it looks at first.

By default it will add one  provider option with possible properties that you can configure which is commented and other is normal option which you need to configure.

Now let’s configure it.

Enter the values for host, accessKey, port (the SSL port should be 6380), and set SSL to true/false. You can obtain these values from the azure portal blade for the cache instance.

Note that the non-SSL port is disabled by default for new caches but you can enable it from portal. For more information about enabling the non-SSL port, see the Access Ports section  of this article.

The following markup shows the changes to the web.config file, specifically the changes to port, host, accessKey, an *ssl.

Now setup and configuration is done. Next step is to create some session variable and try set and get some values to/from that variable


Now launch you site on localhost make sure that you set session value and get those values and display it from somewhere. To check weather it is working or not stop the w3wp.exe worker process from Task manager window and refresh your site then also you will able to get those session values. That’s it your session is now managed in Azure Redis Cache database not in memory where application is hosted.

If you want to see your session data then you can use Redis console which is available in azure redis cache blade.

In above code i have set only one session which is saved as encrypted key in the Redis store. If you want to see exact key value pair of your session you can drill down further with command

“/” is your application name by default. You can set custom application name through provider in web.config file. Like applicationName=”redisTest” then from next time that key becomes {redisTest_asda67ad….}_Data.

That’s it. One last TIPs. One possible error that you could get if you are adding RedisSessionStateProvider Nuget Package in existing web application or even in new application is due to version conflict of MS BCL library that your projects depend on so update your BCL library package from Nuget and then you are good to go. Here is command.

That’s it. This is how you can set Redis Cache for session management in ASP.Net Web application.

Happy Coding!!!