2010-12-03 3 views
0

SMTP 서버 & 로그인 정보가 보호되도록 내 web.config 파일의 elmah 섹션을 어떻게 암호화 할 수 있습니까?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가 열거되어 섹션 만 암호화되고 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"); 
    } 
관련 문제