Replace the values ​​in the settings of the web.config application at run time (for Active Directory)

Some prerequisites ...

I saw the answers using the ConfigurationManager, but I think what I'm trying to achieve is a little different.

I am deploying MVC5 as an Azure cloud service. The customer would like to receive a single delivery package for testing / user acceptance / Live with all (if possible) settings saved in the Azure configuration settings. For our own values, appSettings is not a problem, move them to ServiceConfiguration.cscfg and read them with a call CloudConfigurationManager.GetSetting(which reads from Azure cscfg if it is deployed as Azure, or web.config if it runs locally in IIS during dev)

However, there is still a problem with Microsoft.Web.RedisSessionStateProvider - which only supports reading options from web.config (I emailed Devs and they confirmed it)

I got around this by reading the connection string from ServiceConfiguration.cscfg on Azure and typing it into in-memory web.config as follows, which seemed like a viable way to let MVC do its own session processing configuration.

        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 in Azure Active Directory settings, and I was hoping to do a similar thing, but I cannot access the appSettings section using the same method. Trying to read appSettings as follows gives a null pointer, and indeed, if you are debugging in runtimeConfigInstance, then 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);


    }

, :

  • appSettings , ConfigurationManager ?

  • Active Directory Azure, web.config, , , Azure?

. FederationConfiguration , cscfg, appSettings ida:FederationMetadataLocation ida:Realm ida:AudienceUri web.config ..

        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;
    }
+4
1

.

1: . -.

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;

        }

2.. .

<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>
+1

Source: https://habr.com/ru/post/1614578/


All Articles