1

I am trying to Encrypt sensitive connection string information inside my app.config file for a C# applicaiton that I am developing. I am using the following command from the VS command promt running as administrator:

aspnet_regiis.exe -pef "Credentials" "C:\Users\.....\MyProjectFolderDir"

This is the structure of my app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <configSections>
      <section name="ApplicationSettings" type="sometype"/>
      <section name="WebSettings" type="sometype"/>
      <section name="Credentials" type="sometype"/>
      <section name="SQLServerSettings" type="sometype"/>
    </configSections>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <ApplicationSettings   Mode="mymode"
                           FileSearchMode="myfilemode"
                           SubtractHoursDebugging="0"/>

    <WebSettings WebApiServers=""
                    CredentialMethod="mymethod"/>

    <Credentials
                    Domain="mydomain"
                    UserName="myusername"
                    Password="mypassword"/>

    <SQLServerSettings
        ConnectionString="Server=***********"/>

  </config>

However, I keep getting the following error:

Encrypting configuration section... The configuration section 'Credentials' was not found. Failed!

How can I get this to encrypt my section?

Hooplator15
  • 1,540
  • 7
  • 31
  • 58

3 Answers3

0

Your config file should start with <configuration> element and not <config>. Since it's <config> aspnet_regiis.exe thinning Credentials as nested element and thus the error. With your current config file the command should be

aspnet_regiis.exe -pef "config\Credentials" "C:\Users\.....\MyProjectFolderDir"
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I've tried this, and it still does not work. Can someone try on their machine? – Hooplator15 Jul 14 '16 at 20:43
  • C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -pef "config/Credentials" "C:\Users\Me\Desktop" Encrypting configuration section... The configuration section 'config/Credentials' was not found. Failed! – Hooplator15 Jul 14 '16 at 20:44
0

First of all here's an answer that you can learn from about custom configuration section How to create custom config section in app.config? and here is an example from msdn https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Second, the type usually refer to a real model so you should enter the namespace and the class that you created to model the type of configuration you would like to use like so:

 <configuration>
 <configSections>
  <section name="sampleSection"
           type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1" setting2="value two" 
              setting3="third value" />
</configuration>

Hope it helps

Community
  • 1
  • 1
Erez.L
  • 77
  • 1
  • 10
0

So as it turns out, aspnet-regiis.exe is exclusively for web.config files. It doesn not work with app.config files unless you rename to web.config. Instead of renaming my app.config everytime I wanted to encrypt/decrypt, I ended up creating a class that would handle this each time I ran the application. Make sure you are using the following:

using System.Configuration;
using System.Web.Security;

Class:

internal class Crypto
{
    internal static AppSettingsSection GetEncryptedAppSettingsSection(string exeConfigName)
    {
        // Open the configuration file and retrieve 
        // the connectionStrings section.
        System.Configuration.Configuration config = ConfigurationManager.
            OpenExeConfiguration(exeConfigName);

        AppSettingsSection section =
                config.GetSection("appSettings")
                as AppSettingsSection;

        EncryptConfigSection(config, section);
        return section;
    }

    internal static ConnectionStringsSection GetEncryptedConnectionStringsSection(string exeConfigName)
    {
        // Open the configuration file and retrieve 
        // the connectionStrings section.
        System.Configuration.Configuration config = ConfigurationManager.
            OpenExeConfiguration(exeConfigName);

        ConnectionStringsSection section =
                config.GetSection("connectionStrings")
                as ConnectionStringsSection;

        EncryptConfigSection(config, section);
        return section;
    }

    internal static void EncryptConfigSection(System.Configuration.Configuration config, ConfigurationSection section)
    {
        //Ensure config sections are always encrypted
        if (!section.SectionInformation.IsProtected)
        {
            // Encrypt the section.
            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            // Save the current configuration.
            config.Save();
        }
    }
}
Hooplator15
  • 1,540
  • 7
  • 31
  • 58