How to Use ASP.NET Session State with Windows Azure Web Sites
This topic explains how to use the Windows Azure Cache Service (Preview) to support ASP.NET session state caching.
Without an external provider, session state is stored in-process on the web server hosting the site. For Windows Azure Web Sites, there are two problems with in-process session state. First, for sites with multiple instances, session state stored on one instance is not accessible to other instances. Because a user request can be routed to any instance, the session information is not guaranteed to be there. Second, any changes in configuration could result in the web site running on a completely different server.
The Cache Service (Preview) provides a distributed caching service that is external to the web site. This solves the problem with in-process session state. For more information about how to use session state, see ASP.NET Session State Overview.
The basic steps to use the Cache Service (Preview) for session state caching include:
Create the Cache
At the bottom of the Windows Azure Management Portal, click on the New icon.
Select Data Services, Cache, and then click Quick Create.
Type a unique name for the cache in the Endpoint text box. Then select appropriate values for the other cache properties, and click Create a New Cache.
Select the Cache icon in the Management Portal to view all of your Cache Service endpoints.
You can then select one of the Cache Service endpoints to view its properties. The following sections will use settings from the Dashboard tab to configure Caching for an ASP.NET project.
Configure the ASP.NET project
First, ensure that you have installed the latest Windows Azure SDK for .NET.
In Visual Studio, right-click the ASP.NET project in Solution Explorer, and then select Manage NuGet Packages. (If you are using WebMatrix, click the NuGet button on the toolbar instead)
Type WindowsAzure.Caching in the Search Online edit box.
Select the Windows Azure Caching package, and then click the Install button.
Modify the Web.Config File
In addition to making assembly references for Cache, the NuGet package adds stub entries in the web.config file. To use Cache for session state, several modifications must be made to the web.config.
Open the web.config file for the ASP.NET project.
Find the existing sessionState element and comment it out (or remove it).
Then uncomment the sessionState element that was added by the Windows Azure Caching NuGet package. The end result should look similar to the following screenshot.
Next, find the dataCacheClients section. Uncomment the securityProperties child element.
In the autoDiscover element, set the identifier attribute to your cache's endpoint URL. To find your endpoint URL, go to the cache properties in the Windows Azure Management Portal. On the Dashboard tab, copy the ENDPOINT URL value in the quick glance section.
In the messageSecurity element, set the authorizationInfo attribute to your cache's access key. To find the access key, select your cache in the Windows Azure Management Portal. Then click the Manage Keys icon on the bottom bar. Click the copy button next to the PRIMARY ACCESS KEY text box.
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";
The following code retrieves this value from session state.
object objValue = Session["yourkey"];
if (objValue != null)
strValue = (string)obj;
For more details about how to use ASP.NET session state, see ASP.NET Session State Overview.