Encrypting sections in configuration files


You can face a problem with your connection strings or other confidential and critical information is stored in configuration file when hosting web site or distributing application to clients.

You have many solution options in this case like using SSO, SSS, store encrypted data in a database. But these methods are time consuming and sometime expensive.

The solution I can offer is to encrypt data in configuration file using System.Configuration library from .NET

Add reference to System.Configuration to your project
and run this code once
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Get the section.
ConfigurationSection section =
config.GetSection("connectionStrings");
// Protect (encrypt)the section.
section.SectionInformation.ProtectSection(
"RsaProtectedConfigurationProvider");

// Save the encrypted section.
section.SectionInformation.ForceSave = true;

config.Save(ConfigurationSaveMode.Full);

You will need to add also using System.Configuration;
this code will encrypt your Connection Strings in configuration section.
The great part of this method is the encryption is transparent, you will not need to decrypt Connection Strings each you need to use them.



Comments

Popular posts from this blog

How to poll database using WCF-SQL adapter

SQL query timeout from application but works fast from SSMS