【问题标题】:Encrypt Web.Config Programmatically in Global.asax在 Global.asax 中以编程方式加密 Web.Config
【发布时间】:2019-01-04 09:39:57
【问题描述】:

如果尚未加密,我想在 Application_Start (global.asax) 期间检查并以编程方式加密我的 web.config。

protected void Application_Start()
{
    EncryptConfig.Encrypt("/Web.config");
}

我的方法

protected internal static void Encrypt(string applicationPath)
{
    private const string Provider = "RSAProtectedConfigurationProvider";
    private const string Section = "connectionStrings";

    var test = applicationPath;

    var confg = WebConfigurationManager.OpenWebConfiguration(applicationPath);
    var confStrSect = confg.GetSection(Section);

    if (confStrSect != null)
    {
        confStrSect.SectionInformation.ProtectSection(Provider);
        //confStrSect.SectionInformation.ForceSave = true;
        //confg.Save(ConfigurationSaveMode.Modified);
        confg.Save();
    }
}

我遇到以下异常,我无法使用 Google 和 StackOverflow 解决

System.Configuration.dll 中出现“System.Configuration.ConfigurationErrorsException”类型的异常,但未在用户代码中处理

附加信息:无法为请求的配置对象创建配置文件。

【问题讨论】:

  • 而不是传递路径,让应用程序找到它。 var confg = WebConfigurationManager.OpenWebConfiguration("~");
  • 感谢已修复
  • 很好的解决方案 - 感谢发布

标签: c# asp.net-mvc encryption


【解决方案1】:

正如 Dbugger 在 cmets 中提到的那样,解决方案是

WebConfigurationManager.OpenWebConfiguration("~"); –  dbugger

【讨论】:

  • 很好的解决方案 - 感谢您的发布。你最后选择了那个方法吗?
  • 它起作用了,但是当我在 Azure 中时,我将所有配置移至 Azure Web Config,因此请注意该文件中的敏感信息。
猜你喜欢
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多