2011-04-27 111 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