Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt a custom section in app/web.config file

I need to encrypt/decrypt custom sections in app.config as well as web.config file. I read that aspnet_regiis can be used for web.config, but i need to do this programatically.

After opening the mappedExeConfiguration, i specify a section as follows:

ConfigurationSection connStrings = config.AppSettings;

to encrypt/decrypt the AppSettings section.

How do i specify the name of the custom section? When i type the name of my custom section after the configurationSection object, intelli-sense does not recognize it. (It only recognizes a few well known sections)

P.S. In my function, i need to take the custom section name as a string parameter.

Example:

e.g.

<Configuration>
   <MyCustomTag> 
       <... data /> 
   </MyCustomTag> 
 </Configuration>

where MyCustomTag is the section i need to encrypt/decrypt.

like image 510
user720694 Avatar asked May 13 '13 09:05

user720694


People also ask

How do I encrypt a section of Web config?

Encrypting a Web Configuration Section To encrypt configuration file contents, use the Aspnet_regiis.exe tool with the –pe option and the name of the configuration element to be encrypted. Use the –app option to identify the application for which the Web.

What are the different sections in a Web config file?

A . config file contains XML that has a configuration element as the root node. Information inside this element is grouped into two main areas: the configuration section-handler declaration area, and the configuration section settings area.


2 Answers

I achieved this by using code I found at http://www.a2zmenu.com/Blogs/CSharp/How-to-encrypt-configuration-file.aspx

I'd paste my code in, but basically it is pretty much identical to the code on that web page, except for changing the application names.

Edit: for a custom section, I'm not sure as I did not need to use it, but you could explore what the config object gives you in the following line.

Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");

Here is my entire UpdateKey() method, which I now realise I adapted a bit from the web page. Maybe it helps.

public static void UpdateKey(string key, string newValue) 
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");
    config.AppSettings.Settings[key].Value = newValue;
    config.Save();
} 

Then after I have saved my key(s), I call

EncryptAppSettings("appSettings");

and perhaps you can adapt the param value to suit there too.

like image 122
S. Baggy Avatar answered Sep 30 '22 20:09

S. Baggy


From CommandPromt of VS 2010 call a command for encrypt:

%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis  -pef "connectionStrings" "YOUR_PROJECT_NAME" -prov "DataProtectionConfigurationProvider"

Decrypt:

%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis  -pdf "connectionStrings" "YOUR_PROJECT_NAME"
like image 38
HaGever Avatar answered Sep 28 '22 20:09

HaGever