Enterprise Library makes it very easy to encrypt its configuration sections, such as to protect a database connection string. The part of your app.config / web.config file that specifies where the public key file is as follows:
<keyAlgorithmStorageProvider xsi:type="FileKeyAlgorithmPairStorageProviderData"
name="File Key Algorithm Storage Provider"
path="C:\MyPath\MyKey.key">
<dpapiSettings xsi:nil="true" />
If you have the need to change the path through code, such as to support a deployment wizard or interface, the following code makes life a lot easier!
To get the current value of “path”:
public static string CurrentLocation()
{
RuntimeConfigurationView view = new RuntimeConfigurationView( EnterpriseLibrary.Context );return f.Path;
}
To set the “path”:
static void SaveLocation(string path)
{
ConfigurationContext context = EnterpriseLibrary.Context;
RuntimeConfigurationView view = new RuntimeConfigurationView( context );
FileKeyAlgorithmPairStorageProviderData f = (FileKeyAlgorithmPairStorageProviderData)view.GetKeyAlgorithmPairStorageProviderData();
f.Path = path;
ConfigurationSettings configSettings = context.GetMetaConfiguration();
configSettings.KeyAlgorithmPairStorageProviderData = f;
context.WriteMetaConfiguration( configSettings );
}
FileKeyAlgorithmPairStorageProviderData f = (FileKeyAlgorithmPairStorageProviderData)view.GetKeyAlgorithmPairStorageProviderData();