2010-12-03 61 views
0

如何加密我的web.config文件的elmah部分,以便SMTP服務器&登錄信息受到保護?加密web.config文件的elmah部分

我學會了如何加密連接字符串,appSettings和其他部分;使用代碼或aspnet_regiis.exe。

但是,當我嘗試加密該部分時,它告訴我該部分未找到。

加密它有一個技巧嗎?

感謝, + M

回答

1

上面的信息是正確的(你需要爲目標的「errorMail」或特定elmah集團的子部分)。然而,解決方案是更多的代碼比需要...

這裏是一個更清潔的解決方案,只使用「elmah/errorMail」。解決方案:

string section = "elmah/errorMail"; 

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); 
// Let's work with the section 
ConfigurationSection configsection = config.GetSection(section); 
if (configsection != null) 
    // Only encrypt the section if it is not already protected 
    if (!configsection.SectionInformation.IsProtected) 
    { 
     // Encrypt the <connectionStrings> section using the 
     // DataProtectionConfigurationProvider provider 
     configsection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
     config.Save(); 
    } 
0

我嘗試使用ASPNET_REGIIS但遇到了麻煩指定部分路徑。切換到一種基於代碼的方法,我列舉了&部分,其中有SectionGroups,只有Sections可以加密,而Elmah是SectionGroup,所以我需要加密elmah SectionGroup下的errorMail部分。我比昨天多了一點。

這是片段,如果是別人的路線是有用的,從的global.asax.cs:

private static void ToggleWebEncrypt(bool Encrypt) 
    { 
     // Open the Web.config file. 
     Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 

     //.... (protect connection strings, etc) 

     ConfigurationSectionGroup gpElmah = config.GetSectionGroup("elmah"); 
     if (gpElmah != null) 
     { 
      ConfigurationSection csElmah = gpElmah.Sections.Get("errorMail"); 
      ProtectSection(encrypted, csElmah); 
     } 

     //.... other stuff 
     config.Save(); 

    } 


    private static void ProtectSection(bool encrypted, ConfigurationSection sec) 
    { 
     if (sec == null) 
      return; 
     if (sec.SectionInformation.IsProtected && !encrypted) 
      sec.SectionInformation.UnprotectSection(); 
     if (!sec.SectionInformation.IsProtected && encrypted) 
      sec.SectionInformation.ProtectSection("CustomProvider"); 
    }