7

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?

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
  • Note that Azure App Settings become environment variables. So if the connection string config entry has support for expanding env variables (e.g. `connectionString="%MY_CONN_STRING%"`), then you might be able to make that work. Or maybe there is an API that lets you programmatically set the conn string, which would also let you set it from an env variable. Note sure if it's possible, but just some thoughts... – David Ebbo Feb 21 '16 at 05:00

4 Answers4

8

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

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
  • 1
    Here is a great resource containing everything you could want to know about configuring redis like this https://github.com/Azure/aspnet-redis-providers/wiki/Configuration – MPavlak Jun 27 '17 at 17:57
8

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

MartinM
  • 1,736
  • 5
  • 20
  • 33
0

Take a look at Scott Hanselman's blog on the subject: http://www.hanselman.com/blog/HowToKeepYourASPNETDatabaseConnectionStringsSecureWhenDeployingToAzureFromSource.aspx

You can store the connection string inside of the app settings within the azure portal and then call them from within your app. This prevents the string from being contained within your source code. You'll want to do the same with your storage keys as well.

In the portal go to the settings of your app, and select "Application Settings". Scroll down on that pane to Connection Strings.

Check out the connection strings section of this page as well: https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/

0

I think IPartitionResolver can come to rescue here...

You can basically create a class that implements System.Web.IPartitionResolver interface and specify it in the sessin state configuration in web.config as this

<sessionState mode="Custom"  partitionResolverType="WebAppConnectionStringResolver">

And then in the class you can override the connection string

public class WebAppConnectionStringResolver : System.Web.IPartitionResolver
{
   public void Initialize()
   {

   }

   public string ResolvePartition(object key)
   {
     return System.Configuration.ConfigurationManager.ConnectionStrings["your_Conn_string_name_in_portal"]
   }
}

I havent tested this out but I believe this should work

Read more at

Puneet Gupta
  • 2,237
  • 13
  • 17