Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define connection string for session state in Azure

I am using the RedisSessionStateProvider using a procedimient like this https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-session-state-caching/

I define its connection string in web.config, in this example is XXXXXX.

 <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.5" />
    <globalization culture="es-CO" uiCulture="es" />
    <customErrors mode="Off" />
    <sessionState mode="Custom" customProvider="SessionStateStore">
      <providers>
        <add name="SessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="XXXXXX" throwOnError="true" applicationName="NominappSession" />
      </providers>
    </sessionState>
  </system.web>

I dont want to put the connection string in the source code. So how can i using the settings in Azure to define this connection string?

I deploy to azure from github, so it uses Kudu. I dont have an external CI server.

Any advice please?

like image 458
Ricardo Polo Jaramillo Avatar asked Feb 20 '16 21:02

Ricardo Polo Jaramillo


2 Answers

I did it :)

To do that you need to define the connection string as a environment variable, not as a typical connection string. And in the sesion state provide use the environment variable name.

This way:

  <appSettings>
    <add key="REDIS_CONNECTION_STRING" value="redis,allowAdmin=true" />
  </appSettings>
  <system.web>
    <sessionState mode="Custom" customProvider="SessionStateStore">
      <providers>
        <add name="SessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="REDIS_CONNECTION_STRING" applicationName="REDIS_SESSION_APPLICATION_NAME" throwOnError="true" />
      </providers>
    </sessionState>
  </system.web>

Using that you can now define the connection string in the App Settings of Azure WebSites

like image 161
Ricardo Polo Jaramillo Avatar answered Dec 02 '22 15:12

Ricardo Polo Jaramillo


If you just want to be able to provide your connection string from your source code, you can use the settingsClassName and settingsMethodName properties in the config, like so:

 <sessionState mode="Custom" customProvider="RedisSessionStateStore">
  <providers>
      <add
        name="RedisSessionStateStore"
        type="Microsoft.Web.Redis.RedisSessionStateProvider"
        settingsClassName="MyApp.SessionStateRedisSettings,MyApp"
        settingsMethodName="ConnectionString" />
  </providers>

In here, the settingsClassName is the name of a class in your app, with its fully qualified namespace. The settingsMethod name is the name of the method on this class, which must be static, take 0 parameters and return an string. For example:

namespace MyApp
{
    public static class SessionStateRedisSettings
    {
        public static string ConnectionString()
        {
            return "ConnectionString goes here";
        }
    }
}

From here: https://github.com/Azure/aspnet-redis-providers/wiki/Configuration

like image 22
MartinM Avatar answered Dec 02 '22 16:12

MartinM