How to encrypt user.settings

I am developing a Windows desktop application with C # .NET4.0 VS2010 on Windows 8.1. I have a number of settings that I store using the .NET settings mechanism. They have a user scope, so when they are installed in the application, they are written to "Users \ username \ AppData \ Local \ companyname \ App.exe_URL_randomstuff \ versionno \ user.config.

These options include some user registration information that I need to hide. My research shows that I should be able to encrypt settings with RsaProtectedConfigurationProvider, but all the examples I found for this relate to app.config encryption, not user.config (like http://msdn.microsoft.com/ en-us / library / system.configuration.rsaprotectedconfigurationprovider.aspx ).

My question, therefore, can be encrypted by user.config, and if so, how? I note that when I instantiate a System.Configuration.Configuration object, I can set ConfigurationUserLevel to PerUserRoamingAndLocal. When I check the object through the debugger, it seems to refer to the correct user.config file, but when I go to the instance, the ConfigurationSection returns null to protect it. The code is as follows:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal); ConfigurationSection connStrings = config.AppSettings; connStrings.SectionInformation.ProtectSection(provider); 

I think config.AppSettings is probably incorrect, but I'm not sure what to replace it with.

Any advice was greatly appreciated.

+6
source share
1 answer

Now it works. I was right to use ConfigurationUserLevel.PerUserRoamingAndLocal to access my user.config file. The problem was in config.AppSettings. I was on the right track by replacing this with config.GetSection ("Progname.Properties.Settings"), but I got the wrong name. Now the working code is as follows:

 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal); ConfigurationSection connStrings = config.GetSection("userSettings/Progname.Properties.Settings"); connStrings.SectionInformation.ProtectSection(provider); 

"Progname" is what is called your build. Thanks to @neoistheone and @hatchet for your input.

+7
source

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


All Articles