Unrecognized attribute 'configProtectionProvider' after app.config encryption

I run the following method at the beginning of my application, passing in the section located in the applicationSettings section:

public static void EncryptConfigSection(string sectionKey) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection(sectionKey); if (section != null) { if (!section.SectionInformation.IsProtected) { if (!section.ElementInformation.IsLocked) { section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(sectionKey); } } } } 

Here is an example section in app.config:

 <applicationSettings> <Example.Properties.Settings> <setting name="Key" serializeAs="String"> <value>Value</value> </setting> </Example.Properties.Settings> </applicationSettings> 

When I try to access any of the parameters from a section, I get the following error:

Unrecognized attribute 'configProtectionProvider'

This is a desktop application that needs to encrypt some settings at startup, then decrypt on exit.

Does anyone have a solution to this problem?

+6
source share
5 answers

I could not encrypt / decrypt the configuration file while the application was running and continue reading the values.

Although this is not what I wanted, the solution to the problem was to first encrypt / decrypt .config before launching the application.

Here is another approach that I did not do, but seemed interesting: Encrypting passwords in the .NET app.config file

+2
source

According to this blog post, the fix is ​​to call RefreshSection() on the parent:

 RefreshSection("applicationSettings") 

Unrecognized attribute configProtectionProvider

+3
source

I managed to get Rick Schott to respond with one important caveat - you cannot use the static version of ConfigurationManager.GetSection to retrieve the section after the upgrade - you should use Configuration.GetSection instead.

Full code:

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection("passwordSection") as ConfigurationSection; if (!section.SectionInformation.IsProtected) { // Encrypt the section. section.SectionInformation.ProtectSection("DPAPIProtection"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Modified); // The passwords are now encrypted. // Refresh the *parent* of the section that your passwords are in. ConfigurationManager.RefreshSection("configuration"); // Now use Configuration.GetManager to retrieve the new password section. // This doesn't throw the Configuration Exception :) ConfigurationSection section2 = config.GetSection("passwordSection") as ConfigurationSection; } 
+3
source

I found this: http://andybrennan.wordpress.com/2014/06/05/unrecognized-attribute-configprotectionprovider-after-encrypting-app-config/ . And that solves the problem.

Just use this method as written on the blog:

 private void ResetConfigMechanism() { typeof(ConfigurationManager) .GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, 0); typeof(ConfigurationManager) .GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, null); typeof(ConfigurationManager) .Assembly.GetTypes() .Where(x => x.FullName == "System.Configuration.ClientConfigPaths") .First() .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, null); } 

Call it after saving / updating the configuration.

+3
source

This post just saved my day and just in case anyone needs a fix in vb and 2017

  Private Sub ResetConfigMechanism() GetType(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, 0) GetType(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, Nothing) GetType(ConfigurationManager).Assembly.GetTypes().Where(Function(x) x.FullName = "System.Configuration.ClientConfigPaths").First().GetField("s_current", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, Nothing) End Sub 
0
source

Source: https://habr.com/ru/post/899847/


All Articles