---
title: Storing session state data in an Azure environment
related:
  - https://docs.kentico.com/13/deploying-websites/running-xperience-on-microsoft-azure/deploying-to-azure-web-apps/scaling-azure-web-apps.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

If you [scale](https://docs.kentico.com/13/deploying-websites/running-xperience-on-microsoft-azure/deploying-to-azure-web-apps/scaling-azure-web-apps.md) your projects to multiple instances, you must configure how session state information is stored. Session state must be stored either in the database, which is shared among the servers, or in a special service. Session state cannot be stored in the server's memory – session information is lost whenever the request gets routed to another instance.

It is not necessary to configure shared session state between the live site and administration applications. However, you need to set up the session state storage for your MVC application if you want to scale it to use multiple Azure instances.

You have the following options for storing session state information in the Azure environment:

- [Microsoft Azure SQL Database](#storing-session-state-information-in-azure-sql-database) – easy to set up, suitable for small projects or projects with read access to web pages.
- [Microsoft Azure Redis Cache](#storing-session-state-information-in-azure-redis-cache) – a dedicated cache service appropriate for more demanding projects. See the [Azure Redis Cache Documentation](https://docs.microsoft.com/en-us/azure/redis-cache/).

## Storing session state information in Azure SQL Database

You need to perform the following steps to share session state across the scaled application:

1. Open your project's solution in Visual Studio.
2. Right-click the web project and select **Manage NuGet packages**.
3. Install the **Microsoft.AspNet.Providers** package.
4. Open the **web.config** file.
5. Follow the instructions in the code comments of the **sessionState** section.

After this, your project is configured to store session state information in the Microsoft Azure SQL Database.

## Storing session state information in Azure Redis Cache

To configure your application to use Azure Redis Cache:

1. Create a new Redis Cache according to the instructions in the [Use Azure Redis Cache with a .NET application](https://docs.microsoft.com/en-us/azure/redis-cache/cache-dotnet-how-to-use-azure-redis-cache) article.
2. Open your project's solution in Visual Studio.
3. Right-click the solution and select **Manage NuGet Packages**.
4. Search for and install the **Microsoft.Web.RedisSessionStateProvider** package.

   - This action automatically adds all required assembly references to the project and creates the following section in the web.config file:

     ```xml

     <sessionState mode="Custom" customProvider="MySessionStateStore">
       <providers>
         <!--
           <add name="MySessionStateStore" 
             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]
             connectionTimeoutInMilliseconds = "5000" [number]
             operationTimeoutInMilliseconds = "5000" [number]
           />
         -->
         <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="mycache.redis.cache.windows.net"
         accessKey="..." ssl="true" />
       </providers>
     </sessionState>

     ```
   - Specify the **host** name and **accessKey** values which you can find in the properties of the cache service on Azure.

     > **Tip:** Instead of inserting the values of the _host_ and _accessKey_ attributes directly, you can specify them as key-value pairs in the _appSettings_ section of the application's web.config file. You can then reference the values anywhere in the configuration file using their assigned keys.
     >
     > ```csharp
     >
     > <add key="MyRedisAccessKey" value="...">
     >
     > ```
     >
     > Values stored in the _appSettings_ section can be easily accessed and managed, for example, via the Azure Portal. The contents of the section can also be encrypted, preventing untrustworthy people from accessing application secrets, such as the Redis cache provider access key.
     >
     > See the official [AspNet Redis documentation](https://github.com/Azure/aspnet-redis-providers/wiki/Configuration) for details.

You can now deploy your project to Azure and start utilizing Azure Redis Cache as a session state provider.
