0

我已经使用DataProtectionConfigurationProvider来加密 web 配置的连接字符串,这在本地运行良好。

但是当我将代码上传到生产环境时,网络配置没有得到加密。

我使用了以下代码:

Configuration config =
                WebConfigurationManager.OpenWebConfiguration("/");
            // Let's work with the <connectionStrings> section
            ConfigurationSection connectionStrings = config.GetSection("connectionStrings");
            if (connectionStrings != null)
            {

                // Only encrypt the section if it is not already protected
                if (!connectionStrings.SectionInformation.IsProtected)
                {
                  // Encrypt the <connectionStrings> section using the 
                    // DataProtectionConfigurationProvider provider
                    connectionStrings.SectionInformation.ProtectSection(
                        "DataProtectionConfigurationProvider");
                    config.Save();
                }
            }

我通过放置日志来跟踪代码,发现!connectionStrings.SectionInformation.IsProtected条件不起作用。

任何帮助都将不胜感激!!

4

1 回答 1

0

问题是由于WebConfigurationManager.OpenWebConfiguration("/")中的“/”路径造成的。我的应用程序托管在虚拟目录中。

使用下面的代码解决了这个问题:

  Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
于 2017-03-02T10:58:26.567 回答