2011-02-12 52 views
3

我正在使用C#& WPF和當然還有一個app.config文件。我一直無法找到存儲在app.config中的加密連接字符串的示例。有很多ASP.NET和web.config的示例,但對於app.config沒有任何實質性的東西。我遇到的唯一例子清楚地表明,該字符串在它首次加密的同一臺機器上只能「解碼」(即使是一個字)。在app.config中使用加密連接字符串(或其他數據)有沒有可行的選項?在非ASP.Net應用程序中加密連接字符串

回答

5

Encrypt ConnectionStrings in App.config

private void ProtectSection(String sSectionName) 
{ 
    // Open the app.config file. 
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    // Get the section in the file. 
    ConfigurationSection section = config.GetSection(sSectionName); 
    // If the section exists and the section is not readonly, then protect the section. 
    if (section != null) 
    { 
    if (!section.IsReadOnly()) 
     { 
     // Protect the section. 
      section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); 
      section.SectionInformation.ForceSave = true; 
      // Save the change. 
      config.Save(ConfigurationSaveMode.Modified); 
     } 
    } 
} 
+1

我以前碰到的例子是這樣的。但我的理解是,這些部分只能加密,然後在同一臺機器上解密。所以它不適用於分佈式應用程序。 – 2011-02-12 01:27:45