4
votes

Some background...

I've seen answers using ConfigurationManager, but I think what I'm trying to achieve is slightly different.

I am deploying MVC5 as Azure cloud service. The customer would like a single delivery package for Test/User Acceptance/Live with all (if possible) settings stored in Azure configuration settings. For our own appSettings values this isn't a problem, move them to the ServiceConfiguration.cscfg and read them with a call to CloudConfigurationManager.GetSetting (which reads from Azure cscfg if deployed in an Azure role, or web.config if running locally in IIS during dev)

However, there was still a problem with Microsoft.Web.RedisSessionStateProvider - which only supports reading settings from web.config (I emailed the Devs and they confirmed this)

I got around this by reading the connectionstring from the ServiceConfiguration.cscfg on Azure and stuffing it into the in-memory web.config as follows, which seemed a viable way of allowing MVC to do it's own configuration of the Session handling.

        private void UpdateConfigurationSettings()
    {
        // Obtain the RuntimeConfig type.
        var runtimeConfig = Type.GetType("System.Web.Configuration.RuntimeConfig, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

        // Obtain the RuntimeConfig instance.
        var runtimeConfigInstance = runtimeConfig.GetMethod("GetAppConfig", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);

        UpdateRedisSessionStateSettings(runtimeConfig, runtimeConfigInstance);
        UpdateAppSettings(runtimeConfig, runtimeConfigInstance);

    }

    private static void UpdateRedisSessionStateSettings(Type runtimeConfig, object runtimeConfigInstance)
    {
        // Obtain the SessionStateSection instance.
        SessionStateSection sessionStateSection =
            (SessionStateSection)
                runtimeConfig.GetProperty("SessionState", BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(runtimeConfigInstance, null);

        // Since the SessionStateSection is set to read only be dafault, we must make it writeable.
        typeof (ConfigurationElement).GetField("_bReadOnly", BindingFlags.NonPublic | BindingFlags.Instance)
            .SetValue(sessionStateSection, false);


        // Get the provider from the SessionStateSection
        var provider = sessionStateSection.Providers[0];

        // Since the provider is set to read only be dafault, we must make it writeable.
        typeof (ConfigurationElement).GetField("_bReadOnly", BindingFlags.NonPublic | BindingFlags.Instance)
            .SetValue(provider, false);

        // Update the connection string paramter of the provider
        provider.Parameters.Set("connectionString", ConfigurationParameters.RedisCacheConnectionString);

        // Just read it back - this is only for debugging so I can see that the value has been updated.
        var c = provider.Parameters["connectionString"];
    }

The problem is Active Directory

My real problem now is the Azure Active Directory settings and I was hoping to do a similar thing, but I can't seem to access the appSettings section using the same method. Attempting to read the appSettings as follows gives a null pointer, and indeed if you debug into runtimeConfigInstance every other section is available - but not appSettings !

        private void UpdateAppSettings(Type runtimeConfig, object runtimeConfigInstance)
    {
        // Obtain the AppSettings section instance.
        AppSettingsSection appSettingsSection =
            (AppSettingsSection)
                runtimeConfig.GetProperty("appSettings", BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(runtimeConfigInstance, null);

        // Since the AppSettings section is set to read only be dafault, we must make it writeable.
        typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.NonPublic | BindingFlags.Instance)
            .SetValue(appSettingsSection, false);


    }

So my question has 2 sides:

  1. Is it possible to access appSettings in the same manner without using ConfigurationManager and saving the file ?

  2. Is there any other way of reading the Active Directory definition from Azure settings rather than in web.config to give us a single package deliverable where all settings are configurable on Azure ?

Note : I'm using the following to manipulate the FederationConfiguration with values read from cscfg, but the appSettings keys ida:FederationMetadataLocation ida:Realm ida:AudienceUri in the web.config need to be correct too..

        private void FederatedAuthenticationOnFederationConfigurationCreated(object sender, FederationConfigurationCreatedEventArgs args)
    {
        args.FederationConfiguration.IdentityConfiguration.AudienceRestriction.AllowedAudienceUris[0] = new Uri(ConfigurationParameters.ActiveDirectoryAudienceUri);
        args.FederationConfiguration.WsFederationConfiguration.Issuer = ConfigurationParameters.ActiveDirectoryIssuer;
        args.FederationConfiguration.WsFederationConfiguration.Realm = ConfigurationParameters.ActiveDirectoryRealm;
    }
1

1 Answers

1
votes

Here is the solution for you.

Step 1: Write your custom code on how to read your connection string. In this example I am first reading from the cloud configuration and falling back on web config.

using System;
using System.Configuration;
using Microsoft.Azure;

namespace Namespace1 {
    public class RedisConnectionStringProvider {

        public static string GetConnectionString() {
            const string redisConnectionString="redisConnectionString";
            var cloudConfigurationValue = CloudConfigurationManager.GetSetting(redisConnectionString);
            if (!String.IsNullOrEmpty(cloudConfigurationValue))
                return cloudConfigurationValue;

            var connectionStringSettings = ConfigurationManager.ConnectionStrings[redisConnectionString];

            if (connectionStringSettings == null) {
                throw new ConfigurationErrorsException("A connection string is expected for " + redisConnectionString);
            }

            return connectionStringSettings.ConnectionString;

        }

Step 2. Now, tell the provider to invoke your custom implementation for connection string.

<sessionState mode="Custom" timeout="60" customProvider="RedisSessionProvider">
      <providers>
        <add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" settingsClassName="Namespace1.RedisConnectionStringProvider, AssemblyName, Culture=neutral" settingsMethodName="GetConnectionString"/>
      </providers>
</sessionState>