Шифрование раздела elmah файла web.config - PullRequest
0 голосов
/ 03 декабря 2010

Как мне зашифровать раздел elmah в моем файле web.config, чтобы защитить SMTP-сервер и информацию для входа?

Я узнал, как зашифровать строки подключения, appSettings и другие разделы;используя код или aspnet_regiis.exe.

Однако, когда я пытаюсь зашифровать раздел, он говорит мне, что раздел не найден.

Есть ли способ зашифровать его?

Спасибо, + M

Ответы [ 2 ]

1 голос
/ 05 марта 2012

Приведенная выше информация верна (вам нужно указать адрес «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 голосов
/ 04 декабря 2010

Я пытался использовать aspnet_regiis, но не смог указать путь к разделу. Переходя к подходу, основанному на коде, я перечислил разделы и узнал, что есть SectionGroups, что только Sections могут быть зашифрованы, и что Elmah является SectionGroup, поэтому мне нужно зашифровать раздел errorMail в elmah SectionGroup. Я знаю немного больше, чем вчера.

Это фрагмент, если он полезен кому-то еще в будущем, из 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");
    }
...