【发布时间】:2010-12-03 06:15:32
【问题描述】:
如何加密 web.config 文件的 elmah 部分,从而保护 SMTP 服务器和登录信息?
我已经学会了如何加密连接字符串、appSettings 和其他部分;使用代码或 aspnet_regiis.exe。
但是,当我尝试加密该部分时,它告诉我该部分未找到。
加密有诀窍吗?
谢谢, +M
【问题讨论】:
标签: encryption web-config elmah
如何加密 web.config 文件的 elmah 部分,从而保护 SMTP 服务器和登录信息?
我已经学会了如何加密连接字符串、appSettings 和其他部分;使用代码或 aspnet_regiis.exe。
但是,当我尝试加密该部分时,它告诉我该部分未找到。
加密有诀窍吗?
谢谢, +M
【问题讨论】:
标签: encryption web-config elmah
以上信息是正确的(您需要针对“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();
}
【讨论】:
我尝试使用 aspnet_regiis,但无法指定部分路径。切换到基于代码的方法,我枚举了部分并了解到有 SectionGroups,只有部分可以加密,并且 Elmah 是一个 SectionGroup,所以我需要加密 elmah SectionGroup 下的 errorMail 部分。我比昨天知道的多一点。
这是 sn-p,如果它对其他人有用,来自 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");
}
【讨论】: