Search code examples
asp.netsessionazurecachingazure-sql-database

Using SQL as Session Cache in Azure


I am currently looking for the best way to use session caching in an Azure Webservice. Because multiple Instances are used, standard asp.net Session state does not work.

I found some documents that this can be solved by using an external session state provider which allows using a shared cache (Redis cache) or SQL as an external provider.

But while I found a lot of documentations how to configure the Redis cache, I did not find a single example how to configure an Azure-SQL-Database for caching.

If this is still supported in Azure: Can you provide an example?


Solution

  • You can do this by:

     <sessionState mode="SQLServer"
    sqlConnectionString="Server=tcp:[serverName].database.windows.net;Database=myDataBase;User ID=[LoginForDb]@[serverName];Password=[password];Trusted_Connection=False;Encrypt=True;"
    cookieless="false" timeout="20" allowCustomSqlDatabase="true" />
    

    A nicer solution is to use table storage, because you can specify the lifetime of an object in table storage.

    more info: managing-session-state-in-windows-azure-what-are-the-options