【问题标题】:encrypting web.config加密 web.config
【发布时间】:2011-06-04 09:36:42
【问题描述】:

这是我的 C# 代码:

System.Configuration.Configuration config =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");

ConfigurationSection section = config.GetSection("appSettings");
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;        
config.Save(ConfigurationSaveMode.Modified);
string sectionXml = section.SectionInformation.GetRawXml();

我需要以编程方式加密web.config,但它给了我错误:

无法为请求的配置对象创建配置文件。

【问题讨论】:

  • 这个错误发生在哪??
  • config.Save(ConfigurationSaveMode.Modified);发生了

标签: c# asp.net web-config asp.net-4.0


【解决方案1】:

错误出现在~/web.configOpenWebConfiguration 需要完整的应用程序路径,而不是 web.config 的名称,如 web 上显示的那样。

试试这个(经过测试并为开场工作):

OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

或(基于 msdn 示例代码)

OpenWebConfiguration(/web.config);

甚至用 null 调用它,如 MSDN notes

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 2014-10-26
    • 1970-01-01
    • 2016-12-30
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    相关资源
    最近更新 更多