Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to protect a single element in the appSettings section instead of the entire section?

I would like to protect one key/value pair in my appSettings but not the others using something like I've previously done with the ProtectSection method as seen below.

var configurationSection = config.GetSection("appSettings");
configurationSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

Ideally I would like to do something like the following:

var configurationElement = config.GetSection("appSettings").GetElement("Protected");
configurationElement.ElementInformation.ProtectElement("DataProtectionConfigurationProvider");

Here is the example appSettings I would be operating on:

<configuration>
<appSettings>
    <add key="Unprotected" value="ChangeMeFreely" />
    <add key="Protected" value="########"/>
</appSettings>
</configuration>

I've been searching but haven't found a way to do this. Is this possible?

like image 833
Ross Hambrick Avatar asked Apr 28 '10 19:04

Ross Hambrick


1 Answers

Not out of the box - .NET offers you the ability to encrypt sections - but not individual elements. However, since those are only strings, it's absolutely possible for you yourself to create some scheme to encrypt the string before saving it to file, and decrypt it after it's been read from the config file.

But this won't be transparent - you'll have to do it yourself and you have to do it explicitly.

like image 159
marc_s Avatar answered Oct 06 '22 01:10

marc_s