Encrypting connectionStrings section - utility for app.config
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);
}
}
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");
}
}
}
PowerShell implementation based on MichelZ's answer:
<#
.SYNOPSIS
Encrypts a section in .NET app configuration file.
#>
function Protect-DotNetConfigSection
{
[CmdletBinding()]
param
(
# Path to .exe file.
[Parameter(Mandatory = $true)]
[string] $ExePath,
# List of section names.
[Parameter(Mandatory = $true)]
[string[]] $Sections
)
$config = [System.Configuration.ConfigurationManager]::OpenExeConfiguration($ExePath)
foreach ($section in $Sections)
{
$config.GetSection($section).SectionInformation.ProtectSection('DataProtectionConfigurationProvider')
}
$config.Save()
}
Protect-DotNetConfigSection 'C:\MyApp\MyApp.exe' 'connectionStrings'
Protect-DotNetConfigSection 'C:\MyApp\MyApp.exe' @('connectionStrings', 'appSettings')