无法在 .net 4.5 中加密配置文件,但能够在 .net 3.5 中执行相同操作

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

我有一个 winform 应用程序,我想在配置 file.Hence 中存储一些值,我创建了一个 app.config file.Below 是它的内容。

  <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>

我也想加密数据,所以用下面的代码加密:

 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);

这在我编译和 运行 在 VS 中使用 .net 3.5 的代码时工作正常 2008.But 当我在 VS 2012 中使用 .net 4.5 编译代码时,它给我以下错误section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider") line.Also,将值添加到配置文件后,当我尝试保存文件时,它给了我同样的错误。

The entry 'DataProtectionConfigurationProvider' has already been added.

这是什么原因?

该提供程序在 machine.config 4.0 框架中定义...

<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>

app.config.

中无需再次添加