如何加密app.config?
在wpf中创建app.config(c#)
用过的代码C#:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConnectionStringsSection conStr = config.ConnectionStrings; if (!conStr.SectionInformation.IsProtected) { conStr.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); conStr.SectionInformation.ForceSave = true; config.Save(); } else { foreach (ConnectionStringSettings ss in conStr.ConnectionStrings) Console.WriteLine(ss); Console.Read(); }
config.Save();
– 导致exception:
{“无法使用提供程序’RsaProtectedConfigurationProvider’加密’connectionStrings’部分。来自提供程序的错误消息:对象已存在。\ r \ n”}}
检查SectionInformation.ProtectSection方法
还请看这里
我在Save上得到了同样的例外。 通过以管理员身份运行应用程序,我能够解决这个问题。
我在我的项目中添加了一个app.manifest文件,并改变了执行级别,如下所示:requestedExecutionLevel level =“requireAdministrator”uiAccess =“false”
这样,我总是以管理员身份运行,并且有权保存加密部分。
您可以查看使用aspnet_regiis.exe
为您执行加密。 请参阅此MSDN链接
这样,您无需编写代码即可执行加密。