加密app.config后无法识别的属性’configProtectionProvider’
我在我的应用程序开始时运行以下方法传递一个生活在applicationSettings下的部分:
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); } } } }
以下是app.config中该部分的示例:
Value
当我尝试访问该部分中的任何设置时,我收到以下错误:
无法识别的属性’configProtectionProvider’
这是一个桌面应用程序,需要在启动时加密某些设置,然后在退出时解密。
有没有人有这个问题的解决方案?
我发现了这个: http : //andybrennan.wordpress.com/2014/06/05/unrecognized-attribute-configprotectionprovider-after-encrypting-app-config/ 。 它解决了这个问题。
只需使用博客上写的这个方法:
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); }
保存/刷新配置后调用它。
根据这篇博客文章,解决方法是在父级上调用RefreshSection()
:
RefreshSection("applicationSettings")
无法识别的属性configProtectionProvider
我设法让Rick Schott的答案工作,有一个重要的警告 – 你不能使用静态版本的ConfigurationManager.GetSection来检索刷新后的部分 – 你必须使用Configuration.GetSection 。
完整代码:
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; }
我无法在应用程序运行时加密/解密配置文件并继续读取值。
虽然不是我想要的,但问题的解决方案是在应用程序运行之前首先加密/解密.config。
这是另一种方法,我没做但看起来很有趣: 加密.NET app.config文件中的密码
这篇文章刚刚保存了我的一天,万一有人需要在vb和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