无法加密.net 4.5中的配置文件,但能够在.net 3.5

时间:2016-06-29 14:39:15

标签: c# encryption configuration .net-4.5 configurationmanager

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

  <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 2008中使用.net 3.5编译和运行代码时,这很好。但是当我使用VS 2012使用.net 4.5编译代码时,它在section.SectionInformation.ProtectSection(“DataProtectionConfigurationProvider”)中给出了以下错误“)line.Also,在为配置文件添加值之后,当我尝试保存文件时,它给了我同样的错误。

The entry 'DataProtectionConfigurationProvider' has already been added.

这是什么原因?

1 个答案:

答案 0 :(得分:0)

该提供程序在4.0 Framework的machine.config中定义...

<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中添加它。