Шифрование раздела elmah файла web.config

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

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

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

Есть ли хитрость в шифровании?

Спасибо, +М


person mos    schedule 03.12.2010    source источник


Ответы (2)


Приведенная выше информация верна (вам нужно настроить таргетинг на «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();
    }
person Ralph N    schedule 05.03.2012

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

Это фрагмент из 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");
    }
person mos    schedule 04.12.2010