从服务器

时间:2016-09-22 22:44:42

标签: c# asp.net .net asp.net-mvc

我使用以下方法加密web.config中的连接字符串:

public static void EncryptConnectionString()
{
    var section = GetConfigSection();

    if (section.SectionInformation.IsProtected) return;

    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
    section.SectionInformation.ForceSave = true;
    section.CurrentConfiguration.Save();

}

private static ConnectionStringsSection GetConfigSection()
{
    var config = WebConfigurationManager.OpenWebConfiguration("~");
    var section = config.GetSection("connectionStrings");
    if (section.ElementInformation.IsLocked || section.SectionInformation.IsLocked) return null;

    return section as ConnectionStringsSection;
}
  • 我在Global.asax Application_Start()方法中调用了此方法
  • 在本地运行时,该方法有效,我可以看到连接字符串已加密。
  • 从服务器运行时,该方法不起作用。我运行应用程序,服务器连接字符串上的web.config保持未加密状态。
  • 在其他地方手动调用该方法似乎会导致它工作。如果我从应用程序的HomeController中调用它,我可以看到连接字符串已经加密。
  • Global.asax Application_Start()中的其他方法似乎有效,因为我可以看到我的注册包被使用。

那么为什么不调用这个编辑web.config的方法,在从Application_Start()调用时工作?

1 个答案:

答案 0 :(得分:0)

首次启动应用程序时调用Application_Start,或者更具体地说,当应用程序收到第一个请求时调用Application_Start。尝试点击Home Controller并让它什么都不做,然后检查web.config是否加密。

相关问题