Here’s another suggestion for Microsoft: give me a nice key/value pair configuration section.
Now, you might say, don’t you already have appSettings? The appSettings configuration section is certainly handy, but occasionally, I’ll have to store sensitive data in my configuration file—data required to be encrypted. I’ve made a few attempts to encrypt the appSettings section to no avail, although I’m not sure why it won’t encrypt—the new System.Configuration.AppSettingsSection v2.0 implements System.Configuration.ConfigurationSection, which tends to play nicely with the configuration encryption infrastructure…so either I’m not doing something right or I’m missing something.
You might say, fooey on appSettings, why don’t you put your secret in the connectionStrings section and encrypt that? I could do that (and I know it does play nicely with configuration encryption), but what if I have sensitive data I need to store that’s not a connection string—like a plain old password? That would look kind of silly in a connectionStrings section:
<connectionStrings>
<add name="myPassword" connectionString="bob sent me"/>
</connectionStrings>
In code:
string myPassword = ConfigurationManager.
ConnectionStrings["myPassword"].ConnectionString;
I just don’t think that reads very well.
No, what I really want is a third option: a good ‘ol Key/Value pair configuration section that will encrypt/decrypt consistently. Well, interestingly, two-thirds of the work to make such a section available is already done in the .NET framework.
Most custom configuration sections require that you implement three abstract classes: ConfigurationSection, ConfigurationElementCollection, and ConfigurationElement. As it turns out, the .NET framework already contains a public System.Configuration.KeyValueConfigurationCollection which manages a collection of System.Configuration.KeyValueConfigurationElement objects. The only thing left to do is to expose this collection to configuration via an implementation of ConfigurationSection. It’s as simple as this:
using System.Configuration;
namespace Brad.Configuration
{
public class KeyValueSection : ConfigurationSection
{
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public KeyValueConfigurationCollection Settings
{
get
{
return (KeyValueConfigurationCollection)this[""];
}
set
{
this[""] = value;
}
}
}
}
It’d be nice if such a class were already available in the framework instead of me having to code the last piece. Maybe some nice Microsoft employee can add this class in a future release.