我喜欢更新中定义的键/值AppSettings
的部分Web.config
在运行时。但我不想真正将它们保存到Web.config
file.
我有一个巨大的 Web 应用程序,其中包含许多模块、DLL 和源代码文件。数据库配置、加密密钥、Web 服务的用户名和密码等一系列关键信息都保存在AppSettings
的部分web.config
文件。最近的项目需求需要我将这些值移出web.config
并保存在安全的储存处。
我已经将这些值保护在外部位置,并且可以在应用程序启动时读回它们。
这是示例代码。
全局.asax
public class Global: System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
Dictionary<string, string> secureConfig = new Dictionary<string,string>{};
// --------------------------------------------------------------------
// Here I read and decrypt keys and add them to secureConfig dictionary
// To test assume the following line is a key stored in secure sotrage.
//secureConfig = SecureConfig.LoadConfig();
secureConfig.Add("ACriticalKey","VeryCriticalValue");
// --------------------------------------------------------------------
foreach (KeyValuePair<string, string> item in secureConfig) {
ConfigurationManager.AppSettings.Add(item.Key, item.Value);
}
}
}
正如您可能注意到的那样,更改对的引用是不可行的AppSettings
在由多个编程团队创建的大量代码中,从我的中读取他们的设置secureConfig dictionary
另一方面,我不应该将这些值保存在web.config
文件可供 Web 管理员和操作员、系统管理员和云管理员使用。
为了让程序员的生活更轻松,我想让他们添加自己的价值AppSettings
的部分web.config
在开发期间,但它们将从那里删除并稍后在部署期间放入安全存储中,但是这些值应该可以透明地用于编程,因为它们仍然在AppSettings
部分。
Question: 我怎样才能添加值AppSettings
在运行时,程序可以使用以下方式读取它们ConfigurationManager.AppSettings["ACriticalKey"]
to get "VeryCriticalValue"
而不将它们保存在 Web.Config 中?
请注意: ConfigurationManager.AppSettings.Add(item.Key, item.Value);
给我ConfigurationErrorsException
有消息The configuration is read only.
请注意:最好一些设置应该能够保留AppSettings
像之前一样