Dynamically encrypting user configuration settings file - c#

I have an application that connects to a database. Is there a way to encrypt the file that contains the connection string along with other settings? The file is quite exposed as the database connection password is stored inside the user.config file. The only semi-solution I found was for web applications by adding a web.config file, but I'm not sure if this applies the same way. Is there any documentation regarding configuration encryption?
Here's my attempt so far:
user.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<TimeClock.Properties.Settings>
<setting name="webserver" serializeAs="String">
<value>test.js.com/</value>
</setting>
<setting name="timeclockConnectionString" serializeAs="String">
<value>port=3306;server=localhost;user id=root;password=123456;database=timeclock;convertzerodatetime=True</value>
</setting>
<setting name="screenmode" serializeAs="String">
<value>0</value>
</setting>
</TimeClock.Properties.Settings>
</userSettings>
</configuration>
Method:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
string provider = "RsaProtectedConfigurationProvider";
ConfigurationSection connStrings = config.GetSection("timeclockConnectionString");
connStrings.SectionInformation.ProtectSection(provider);
connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
As you can see, I'm trying to encrypt only the timeclockConnectionString and nothing else. Obviously, I get an error:
Object reference not set to an instance of an object.
which probably stems from the config.GetSection.
Any help would be greatly appreciated.

Related

Reading a string from app.config file

My question isn't related to the connection string which is the most popular. The other only help I could find with this is someone not using the reference.
What I'm trying to do: I have a string stored in the app.config file, which looks like:
<applicationSettings>
<App_5.Properties.Settings>
<setting name="Location_Name" serializeAs="String">
<value>String I want</value>
</setting>
My code to pull the string is:
string Loc_Name = ConfigurationManager.AppSettings["Location_Name"];
I have the reference added, and used. When I run the program my return is always null.
You are storing it in the wrong place in the app.config. For single name/value pairs use the appSettings. Then your retrieval code in the question will work.
<configuration>
<appSettings>
<add key="Location_Name" value="String I want" />
</appSettings>
</configuration>
Code (repeated from question)
var value = ConfigurationManager.AppSettings["Location_Name"];

Read and write settings programmatically in C#

I've got the following example content generated by the Settings-Designer in Visual Studio 2015:
<configuration>
<applicationSettings>
<Test.Properties.Settings>
<setting name="LogFileFolder" serializeAs="String">
<value>logs</value>
</setting>
<setting name="sPluginsFolder" serializeAs="String">
<value>plugins</value>
</setting>
<setting name="sLangBaseName" serializeAs="String">
<value>Test.Resources.Language.Test</value>
</setting>
<setting name="ConfirmationExit" serializeAs="String">
<value>True</value>
</setting>
</Test.Properties.Settings>
</applicationSettings>
</configuration>
Now I want to access, for example, the variable "ConfirmationExit" within my code. In first I tried with Properties.Settings.Default which is working nicely, but I want to retrieve the configuration keys more generic and provide a ConfigurationService which can be used by any other .NET program.
I tried playing around with the ConfigurationManager, but I didn't get the values of the different settings/properties. I always got null if I tried something like this:
string test = ConfigurationManager.AppSettings["ConfirmationExit"];
How can I PROGRAMMATICALLY read AND write properties/settings defined within the app.config file with the use of the Settings-Designer. Thx for any help ;)
UPDATE:
Maybe I have to mention, that there is a namspace used as XML element (I think this is generated by the designer or soemthing else?!):
<Test.Properties.Settings>
inside of
<applicationSettings>
Also please remind:
This is a project structure like this:
1. Project_Common
2. Project_Main
|-- Project_Common
3. Project_Plugin1
|-- Project_Common
4. Project_Plugin2
|-- Project_Common
...
In Project_Common there should be a ConfigurationService which can retrieve all properties of all applications. Maybe the constructor has to have a parameter to inject settings or something.
string test = Properties.Settings.Default.ConfirmationExit;
From MSDN: https://msdn.microsoft.com/en-us/library/a65txexh.aspx
read:
Settings.Default.{propertyName}
so in your case for example
Settings.Default.ConfirmationExit
reads ConfirmationExit-Value
Set it:
Settings.Default.ConfirmationExit = false
Write:
Settings.Default.Save();

C# Changing Web Reference url

I'm trying to change a web reference path depending on whether the site is live or not.
The application settings are in the web.config
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService"
serializeAs="String">
<value>http://localhost:50711/Service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
I've tried the following:
Properties.Settings.Default.WebReferenceName_Service_TBService.Equals("http://www.newurl.com/service.svc");
which although doesn't error, checking it later, shows it hasn't changed.
and
var config = WebConfigurationManager.OpenWebConfiguration("~/web.config");
config.AppSettings["WebReferenceName_Service_TBService"] = "http://www.newurl2.com/service.svc";
config.Save();
but this errors saying I don't have permissions
Is there another way of doing this?
I'd rather not have to use if statements all over the place, as my types are namespaced differently.
e.g.
using (var service = new WebServiceLocal.TheWebServiceService())
{
WebServiceLocal.blah();
}
Thanks
The preferred way to do this is to use config transformations. So your default web.config would contain:
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService" serializeAs="String">
<value>http://localhost:50711/Service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
</applicationSettings>
And in your transform, you would have something like this (note the xdt:Transform="Replace"):
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService" serializeAs="String">
<value xdt:Transform="Replace">http://www.newurl2.com/service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
</applicationSettings>
A point to note, if you are doing something similar for an app.config (as opposed to web.config) you will need to install an addon like SlowCheetah.

Access section 'applicationSettings' (not 'appSettings') in config file from setup

I am in the process of creating a setup for a web application we build. No I have a configuration file, which looks something like like this, which contains a section 'appSettings', and a section 'applicationSettings':
<configuration>
<appSettings>
<add key="Password" value="dummy"/>
<add key="Username" value="dummy"/>
<add key="DB" value="dummy"/>
<add key="DBServer" value="dummy"/>
<add key="LogStoredProcedure" value="dummy"/>
<add key="ErrorStoredProcedure" value="dummy"/>
<add key="ErrorFileName" value="dummy"/>
<add key="EncryptionKey" value="dummy"/>
</appSettings>
<applicationSettings>
<inoBIBooks.My.MySettings>
<setting name="BIDB_Username" serializeAs="String">
<value>Username</value>
</setting>
<setting name="BIDB_Server" serializeAs="String">
<value>Servername</value>
</setting>
<setting name="BIDB_Database" serializeAs="String">
<value>Database</value>
</setting>
<setting name="BIDB_Password" serializeAs="String">
<value>Password</value>
</setting>
</inoBIBooks.My.MySettings>
</applicationSettings>
</configuration>
Now, from my setup I have to open the config file from the file system with
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
where variable 'targetvdir' contains the path to the config file.
I get the config file by this, and I am able to edit the 'appSettings' section by
config.AppSettings.Settings["Password"].Value = "something";
But I am not able to do that anyway with the 'applicationSettings' section. In the web application itself I access that part by
Properties.Settings.Default.<Setting>
but that wont work from my setup project.
Is there a chance to edit the 'applicationSettings' section as easy as the 'appSettings' section? Or do I have to edit the xml itself?
Any hint is much appreciated.
Kind regards,
Kai Hartmann
I apologize for answering my question by myself, since I found the solution right after posting it. This question gave the answer basically: Save and reload app.config(applicationSettings) at runtime
I had to use this code, to write to that section 'applicationSettings':
// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
// set a value to that specific property
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");
applicationSetting.Value.ValueXml.InnerText = "username";
// without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = true;
// save
config.Save();

How to read name-value attributes from an app.config file?

I am able to read the key-value attributes from the app.config file by using the following syntax.
System.Configuration.ConfigurationManager.AppSettings["ConfigurationFile"]
I want to read the name-value attributes from the app.config file which are defined under the following schema.
<applicationsettings>
<Host.Properties.Settings>
<setting name="Path" serializeAs="String">
<value>F:\PATH\</value>
</setting>
<Host.Properties.Settings>
</applicationSettings>
</configuration>
What is the correct solution?
System.Configuration.ConfigurationManager.AppSettings["Path"]
or this:
Host.Properties.Settings.Default.Path
should work.

Categories

Resources