2011-04-27 109 views
19

效用是否有一个实用工具,将类似的方式,人们可以使用aspnet_regiisweb.config加密文件在app.config文件中的命名配置部分(或只是connectionStrings节)?加密connectionStrings节 - 为的app.config

我知道这可以在代码中完成 - 这里有代码示例,但我希望避免为此编写应用程序。

+0

Oded,好奇地知道这个具体的动机? – wal 2011-04-27 11:59:25

+0

@wal - 加急所有连接字符串部分的紧急业务要求。使用'aspnet_regiis'很容易在'web.config'文件上完成,而'app.config'则不那么容易。 – Oded 2011-04-27 12:14:32

+0

如果它的紧急/快速,那么我只能建议通过勾选文件 - >属性高级下的'加密内容以保护数据'来加密整个文件。 :| – wal 2011-04-27 12:50:35

回答

15

你可以尝试以下方法:

http://www.dotnetprofessional.com/blog/post/2008/03/03/Encrypt-sections-of-WebConfig-or-AppConfig.aspx

总之 - 的app.config文件重命名为web.config - 该架构是相同的,因此aspnet_regiis作品。完成后重命名为app.config

+0

-1 - 这只是创建一个带有加密部分的'web.config'文件,当解密时它是空的。它甚至没有看到'app.config'文件。 – Oded 2011-04-27 12:35:10

+0

aspnet_regiis -pef秘密。编辑放弃了。 – RBZ 2011-04-27 12:39:31

+0

这是我第一次尝试。没有丝毫的差异。在发布之前进行测试。 – Oded 2011-04-27 12:40:19

4

老问题,但在这里是微软的方法:

.NET 2.0: http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx

.NET 3.5: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.90).aspx (节 “加密配置文件的部分使用受保护的配置”)

在app.config文件上切换加密:

static void ToggleConfigEncryption(string exeConfigName) 
{ 
    // Takes the executable file name without the 
    // .config extension. 
    try 
    { 
     // Open the configuration file and retrieve 
     // the connectionStrings section. 
     Configuration config = ConfigurationManager. 
      OpenExeConfiguration(exeConfigName); 

     ConnectionStringsSection section = 
      config.GetSection("connectionStrings") 
      as ConnectionStringsSection; 

     if (section.SectionInformation.IsProtected) 
     { 
      // Remove encryption. 
      section.SectionInformation.UnprotectSection(); 
     } 
     else 
     { 
      // Encrypt the section. 
      section.SectionInformation.ProtectSection(
       "DataProtectionConfigurationProvider"); 
     } 
     // Save the current configuration. 
     config.Save(); 

     Console.WriteLine("Protected={0}", 
      section.SectionInformation.IsProtected); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 
+0

你真的错过了这个问题的关键。而这些链接根本没有解决这个问题。 – Oded 2013-01-22 10:07:59

+0

是否需要这一点而不必编写应用程序? – MichelZ 2013-01-22 10:20:06

+0

是的。我正在寻找一个现有的工具。 – Oded 2013-01-22 10:25:20

4

编译这个控制台应用程序,并将一个配置文件拖到它上面。它会将其连接字符串加密的配置文件的副本吐出。

请注意,您必须以使用配置文件的相同用户进行加密。

using System; 
using System.Configuration; 
using System.IO; 

namespace ConnectionStringEncryptor 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args.Length == 0) 
      { 
       throw new ArgumentException("Please supply a config file to encrypt"); 
      } 
      string originalConfigFilePath = args[0]; 
      AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", originalConfigFilePath); 
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
      ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings"); 
      connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
      config.SaveAs(originalConfigFilePath + ".encrypted"); 
     } 
    } 
} 
+1

完美。不要忘记添加对System.Configuration的引用。 – mhenry1384 2016-07-27 15:20:03