December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to encrypt data within a web.config file programmatically.
To encrypt data within a web.config file section, use the following method:
public static void EncryptWebConfigData(string appPath,
string protectedSection,
string dataProtectionProvider)
{
System.Configuration.Configuration webConfig =
WebConfigurationManager.OpenWebConfiguration(appPath);
ConfigurationSection webConfigSection = webConfig.GetSection(protectedSection);
if (!webConfigSection.SectionInformation.IsProtected)
{
webConfigSection.SectionInformation.ProtectSection(dataProtectionProvider);
webConfig.Save();
}
}To decrypt data within a web.config file section, use the following method:
public static void DecryptWebConfigData(string appPath, string protectedSection)
{
System.Configuration.Configuration webConfig =
WebConfigurationManager.OpenWebConfiguration(appPath);
ConfigurationSection webConfigSection = webConfig.GetSection(protectedSection);
if (webConfigSection.Section Information.IsProtected)
{
webConfigSection.SectionInformation.Un protectSection();
webConfig.Save();
}
}You will need to add the System.Web and System.Configuration DLLs to your project before this code will compile.
To encrypt data, you can call the EncryptWebConfigData method with the following arguments:
EncryptWebConfigData("/WebApplication1", "appSettings",
"DataProtectionConfigurationProvider");The first argument is the virtual path to the webapplication, the second ...
Read now
Unlock full access