一些背景……
我已经看到了使用ConfigurationManager的答案,但我认为我想要实现的目标略有不同.
我正在将MVC5部署为Azure云服务.客户希望单个交付包用于测试/用户接受/实时,并在Azure配置设置中存储所有(如果可能)设置.对于我们自己的appSettings值,这不是问题,将它们移动到ServiceConfiguration.cscfg并通过调用CloudConfigurationManager.GetSetting(如果部署在Azure角色中从Azure cscfg读取,或者如果在本地运行则从web.config读取)来读取它们.开发期间的IIS)
但是,Microsoft.Web.RedisSessionStateProvider仍然存在问题 – 它只支持从web.config读取设置(我通过电子邮件发送了Devs并证实了这一点)
我通过从Azure上的ServiceConfiguration.cscfg读取连接字符串并将其填充到内存中的web.config中来解决这个问题,如下所示,这似乎是允许MVC自己配置会话处理的可行方法.
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"];
}
问题是Active Directory
我现在真正的问题是Azure Active Directory设置,我希望做类似的事情,但我似乎无法使用相同的方法访问appSettings部分.尝试按如下方式读取appSettings会产生一个空指针,实际上如果调试到runtimeConfigInstance中,则每个其他部分都可用 – 但不是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);
}
所以我的问题有两个方面:
>是否可以在不使用ConfigurationManager并保存文件的情况下以相同方式访问appSettings?
>是否有其他方法可以从Azure设置而不是web.config中读取Active Directory定义,以便为我们提供单个程序包可交付,其中所有设置都可在Azure上配置?
注意:我使用以下操作来使用从cscfg读取的值来操作FederationConfiguration,但appSettings键ida:FederationMetadataLocation ida:Realm ida:web.config中的AudienceUri也需要正确.
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步:编写有关如何读取连接字符串的自定义代码.在这个例子中,我首先从云配置中读取并回退到web配置.
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>