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.
Follow these directions to create the cache.
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
In addition to making assembly references for Cache, the NuGet package adds stub entries in the web.config file.
- Open the web.config and find the the sessionState element.
Enter the values for
port(the SSL port should be 6380), and set
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.
<system.web>; <customErrors mode="Off" />; <authentication mode="None" />; <compilation debug="true" targetFramework="4.5" />; <httpRuntime targetFramework="4.5" />; <sessionState mode="Custom" customProvider="RedisSessionProvider">; <providers>; <!--<add name="RedisSessionProvider" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] throwOnError = "true" [true|false] retryTimeoutInMilliseconds = "0" [number] databaseId = "0" [number] applicationName = "" [String] />;-->; <add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" port="6380" host="movie2.redis.cache.windows.net" accessKey="m7PNV60CrvKpLqMUxosC3dSe6kx9nQ6jP5del8TmADk=" ssl="true" />; <!--<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="127.0.0.1" accessKey="" ssl="false" />;-->; </providers>; </sessionState>; </system.web>;
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.
For a guide to the change from Websites to App Service see: Azure App Service and Its Impact on Existing Azure Services