Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypting connectionStrings section - utility for app.config

Tags:

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.

like image 850
Oded Avatar asked Apr 27 '11 11:04

Oded


People also ask

How do you encrypt configuration sections?

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.


2 Answers

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.

like image 186
RBZ Avatar answered Nov 10 '22 01:11

RBZ


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);     } } 
like image 29
MichelZ Avatar answered Nov 09 '22 23:11

MichelZ