Is there a utility that will encrypt a named configuration section (or just the connectionStrings
section) in an app.config
file in a similar manner that one can use aspnet_regiis
with web.config
files?
I know this can be done in code - there are code examples out there, but I am hoping to avoid writing an application for this.
Encrypting a Web Configuration Section To encrypt configuration file contents, use the Aspnet_regiis.exe tool with the –pe option and the name of the configuration element to be encrypted. Use the –app option to identify the application for which the Web.
You can try the following:
https://magenic.com/thinking/encrypting-configuration-sections-in-net
In short - rename the app.config
file to web.config
- the schema is identical, so aspnet_regiis
works. Rename back to app.config
when finished.
Old question, but here is the Microsoft way:
.NET 2.0: http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx
.NET 3.5: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.90).aspx (Section "Encrypting Configuration File Sections Using Protected Configuration")
Toggle Encryption on app.config file:
static void ToggleConfigEncryption(string exeConfigName) { // Takes the executable file name without the // .config extension. try { // Open the configuration file and retrieve // the connectionStrings section. Configuration config = ConfigurationManager. OpenExeConfiguration(exeConfigName); ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection; if (section.SectionInformation.IsProtected) { // Remove encryption. section.SectionInformation.UnprotectSection(); } else { // Encrypt the section. section.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider"); } // Save the current configuration. config.Save(); Console.WriteLine("Protected={0}", section.SectionInformation.IsProtected); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With