Search code examples
c#encryptionconfiguration.net-4.5configurationmanager

Not able to encrypt Configuration file in .net 4.5 but able to do the same in .net 3.5


I have a winform application, and I want to store some values in configuration file.Hence, I created an app.config file.Below are its contents.

  <configProtectedData>
    <providers>
      <add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy="" />      
    </providers>
  </configProtectedData>

  <appSettings>

  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

I also want data to be encrypted, hence I use the following code to enrypt it:

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    ConfigurationSection section = config.GetSection(sectionName);

                    if (!section.SectionInformation.IsProtected)
                    {

                        section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    }
                    section.SectionInformation.ForceSave = true;

                    config.Save(ConfigurationSaveMode.Modified);

This works fine when I compile and run the code using .net 3.5 in VS 2008.But when I compile the code using .net 4.5 using VS 2012, it gives me the following error at section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider") line.Also, after adding value to config file, when I try to save the file, it gives me the same error.

The entry 'DataProtectionConfigurationProvider' has already been added.

What is the reason for this?


Solution

  • That provider is defined in the machine.config for the 4.0 Framework...

    <configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
        <providers>
            <add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/>
            <add name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" useMachineProtection="true" keyEntropy=""/>
        </providers>
    </configProtectedData>
    

    There is no need to add it again in the app.config.