2011-02-12 76 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