0

Ich habe eine Winform-Anwendung, und ich möchte einige Werte in der Konfigurationsdatei speichern.Hier habe ich eine app.config-Datei erstellt. Unten sind seine Inhalte.
Nicht in der Lage, die Konfigurationsdatei in .net 4.5 zu verschlüsseln, aber in der Lage, das gleiche zu tun .net 3.5

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

Ich möchte auch Daten verschlüsselt werden, daher verwende ich den folgenden Code, um es enrypt:

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

Dies funktioniert wh de Ich kompiliere und führe den Code mit .net 3.5 in VS 2008. Aber wenn ich den Code mit .net 4.5 mit VS 2012 kompiliere, gibt es mir den folgenden Fehler in der section.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider") Zeile. Auch Nach dem Hinzufügen von Wert zur Konfigurationsdatei, wenn ich versuche, die Datei zu speichern, gibt es mir den gleichen Fehler.

The entry 'DataProtectionConfigurationProvider' has already been added. 

Was ist der Grund dafür?

Antwort

0

Dass Anbieter im machine.config für den 4.0-Framework definiert ist ...

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

Es gibt keine Notwendigkeit, sie wieder in der app.config hinzuzufügen.

+0

Danke, es hat funktioniert. –

+0

Wie kann ich DataProtectionConfigurationProvider Version 2.0 in .net 4.5 verwenden –