如何加密user.settings

时间:2013-11-08 18:59:37

标签: c# .net encryption settings user.config

我正在Windows 8.1上使用C#.NET4.0 VS2010开发Windows桌面应用程序。我使用.NET设置机制存储了一系列设置。它们具有用户范围,因此,在应用程序中设置时,它们将写入Users \ username \ AppData \ Local \ companyname \ App.exe_URL_randomstuff \ versionno \ user.config。

这些设置包括我需要隐藏的一些用户注册信息。我的研究表明我应该能够使用RsaProtectedConfigurationProvider加密设置,但我发现的所有示例都与加密app.config而不是user.config(例如http://msdn.microsoft.com/en-us/library/system.configuration.rsaprotectedconfigurationprovider.aspx)有关。

因此,我的问题是可以加密user.config,如果是这样,怎么办?我注意到当我实例化System.Configuration.Configuration对象时,我可以将ConfigurationUserLevel设置为PerUserRoamingAndLocal。当我通过调试器检查对象时,它似乎引用了正确的user.config文件,但是当我继续实例时,一个ConfigurationSection来保护它返回null。代码如下所示:

System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.AppSettings;

connStrings.SectionInformation.ProtectSection(provider);

我认为config.AppSettings可能不正确,但我不确定要用什么替换它。

任何建议都非常感谢。

1 个答案:

答案 0 :(得分:8)

现在就开始工作了。我使用ConfigurationUserLevel.PerUserRoamingAndLocal来访问我的user.config文件是正确的。问题出在config.AppSettings上。我在正确的轨道上用config.GetSection(“Progname.Properties.Settings”)替换它,但是命名错误了。现在的工作代码如下:

System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.GetSection("userSettings/Progname.Properties.Settings");

connStrings.SectionInformation.ProtectSection(provider);

“Progname”是你的程序集被调用的任何东西。感谢@neoistheone和@hatchet的投入。

相关问题