C# 如何加密user.settings

C# 如何加密user.settings,c#,.net,encryption,settings,user.config,C#,.net,Encryption,Settings,User.config,我正在windows 8.1上使用C#.NET4.0 VS2010开发一个windows桌面应用程序。我有一系列使用.NET设置机制存储的设置。它们具有用户范围,因此在应用程序中设置时,它们会写入Users\username\AppData\Local\companyname\App.exe\u URL\u randomstuff\versionno\user.config 这些设置包括一些我需要隐藏的用户注册信息。我的研究表明,我应该能够使用RsaProtectedConfigurationP

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

这些设置包括一些我需要隐藏的用户注册信息。我的研究表明,我应该能够使用RsaProtectedConfigurationProvider加密设置,但我发现的所有示例都与加密app.config而不是user.config有关(例如)

因此,我的问题是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可能不正确,但我不确定该用什么替换它


非常感谢您的任何建议。

现在可以使用了。我正确地使用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的帮助。

本文将为您提供两个选项:在编写完代码后,您是否正在执行
config.Save()
?@hatchet-是的,我正在使用config.Save(ConfigurationSaveMode.Full)。@neoistheone这是一篇有用的文章(我指的是加密所有内容选项)但是让我想知道如果我使用config.GetSection,我应该给我的节命名什么。我尝试了config.GetSection(“Progname.Properties.Settings”),但它仍然返回null。我想微软应该内置这个选项!在我的例子中,节名是“userSettings/Progname.MySettings”。很抱歉,我不明白,这将如何加密user.settings。你能告诉我这个的用法吗?如何使用它来保护user.config?@Fawad阅读了RsaProtectedConfigurationProvider类。举例来说,一个很好的起点是