Compile this console application, and drag a config file onto it. It will spit out a copy of the config file with its connection strings encrypted.
Note that you must encrypt as the same user who will consume the config file.
using System;
using System.Configuration;
using System.IO;
namespace ConnectionStringEncryptor
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
throw new ArgumentException("Please supply a config file to encrypt");
}
string originalConfigFilePath = args[0];
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", originalConfigFilePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.SaveAs(originalConfigFilePath + ".encrypted");
}
}
}
web.config
files usingaspnet_regiis
, not so easy withapp.config
. – Maculation