2010-12-03 62 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"); 
    }