I want to create a msbuild task which encrypts certain sections of my web.configs. The following code works great inside a weapplication. Running the code as an msbuild causes an error saying it cannot create the config file..
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
I couldn’t find any classes which do the right job. Ideas anyone?
You should create your own custom MSBuild task.
The below code is a custom task.
I’ve made mine application(winforms) capable, but I marked the lines you can change for web based.
I’ve created an abstract class with 2 subclasses to handle encrypt and decryption.
Cheers!
This will help as well:
http://www.codeproject.com/KB/dotnet/EncryptingTheAppConfig.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Encrypting-Connection-String.aspx
But the encapsulation into a MSBuild Task is my contribution.
The second URL above also mentions a command line method:
Here is that quoted material (partial quote that is):::
Encryption/Decryption using aspnet_regiis.exe command line tool
You can also encrypt and decrypt sections in the Web.config file using the aspnet_regiis.exe command-line tool, which can be found in the \Microsoft.Net\Framework\version directory. To encrypt a section of the Web.config using the DPAPI machine key with this command-line tool, use following command.
aspnet_regiis.exe -pe “connectionStrings” -app “/YourWebSiteName” –prov “DataProtectionConfigurationProvider”
To decrypt connectionStrings section using this tool, you can specify following command in aspnet_iisreg.exe tool.
aspnet_regiis.exe -pd “connectionStrings” -app “/YouWebSiteName”