Encryption 加密web.config文件的elmah部分

Encryption 加密web.config文件的elmah部分,encryption,web-config,elmah,Encryption,Web Config,Elmah,如何加密web.config文件的elmah部分,以保护SMTP服务器和登录信息 我已经学习了如何加密连接字符串、appSettings和其他部分;使用代码或aspnet_regiis.exe 但是,当我尝试加密该节时,它告诉我找不到该节 加密有什么诀窍吗 谢谢, +M我尝试使用aspnet\u regiis,但在指定节路径时遇到问题。切换到基于代码的方法,我列举了部分&了解到有部分组,只有部分可以加密,Elmah是一个部分组,所以我需要加密Elmah部分组下的errorMail部分。我知道的比

如何加密web.config文件的elmah部分,以保护SMTP服务器和登录信息

我已经学习了如何加密连接字符串、appSettings和其他部分;使用代码或aspnet_regiis.exe

但是,当我尝试加密该节时,它告诉我找不到该节

加密有什么诀窍吗

谢谢,
+M

我尝试使用aspnet\u regiis,但在指定节路径时遇到问题。切换到基于代码的方法,我列举了部分&了解到有部分组,只有部分可以加密,Elmah是一个部分组,所以我需要加密Elmah部分组下的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");
    }

上述信息是正确的(您需要针对“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();
    }
string section=“elmah/errorMail”;
配置配置=WebConfiguration Manager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
//让我们来处理这个部分
ConfigurationSection configsection=config.GetSection(section);
if(configsection!=null)
//仅当节尚未受到保护时才对其进行加密
如果(!configsection.SectionInformation.IsProtected)
{
//使用
//DataProtectionConfigurationProvider提供程序
configsection.SectionInformation.ProtectSection(“DataProtectionConfigurationProvider”);
config.Save();
}