This library helps you to keep your config files secure because once you call the Encrypt method the config sections and connections strings will be encrypted using a specific value from the machine (or devices) running your code.
- π Encrypt ConfigSections and ConnectionStrings inside your config files
- π Decrypt config sections and connectionStrings
- βοΈ Update configuration values directly on your config file and encrypt
- π Get secure access to all your parameters based on your environments
- π Get easy access to shared settings values
First of all you need to download the latest version of the library, once you have downloaded you should save into a specif folder and include in your project, finally you need to import in to your dependency
In references righ-click and then browse for your library
First, you need to config your main app.config in order to add the configSections, you should have at leas your file like thise example
This is an example of how you can set up your main app.config or web.config
<?xml version = "1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name = "EnvironmentSettings" type="System.Configuration.ApplicationSettingsGroup">
<section name = "Production" type="System.Configuration.AppSettingsSection"/>
<section name = "Development" type="System.Configuration.AppSettingsSection"/>
<section name = "QA" type="System.Configuration.AppSettingsSection"/>
</sectionGroup>
</configSections>
<connectionStrings>
<add name = "MovieDBContextPrd"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesPrd.mdf"
providerName="System.Data.SqlClient">
<add name = "MovieDBContextDev"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesDev.mdf"
providerName="System.Data.SqlClient">
<add name = "MovieDBContextQA"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesQA.mdf"
providerName="System.Data.SqlClient">
</connectionStrings>
<EnvironmentSettings>
<Production file = "Config\Production.config" ></ Production >
<Development>
<add key = "TestKey" value="DevValue"/>
<add key = "connection" value="MovieDBContextDev"/>
</Development>
<QA>
<add key = "TestKey" value="QAValue"/>
<add key = "connection" value="MovieDBContextQA"/>
</QA>
</EnvironmentSettings>
<appSettings>
<add key = "CurrentEnvironment" value="Development"/>
<add key = "CommonKey" value="CommonValue"/>
</appSettings>
</configuration>
There are specific key sections than you can change in order to get your specific parameters values
ConfigSections
This is the main configuration that you need to add in your main config file, you can name your sections name as you wish, but the sectionGroup name must be EnvironmentSettings ,
<configSections>
<sectionGroup name = "EnvironmentSettings" type="System.Configuration.ApplicationSettingsGroup">
<section name = "Production" type="System.Configuration.AppSettingsSection"/>
<section name = "Development" type="System.Configuration.AppSettingsSection"/>
<section name = "QA" type="System.Configuration.AppSettingsSection"/>
</sectionGroup>
</configSections>
EnvironmentSettings
This sections are used to specify your custom parameters based on your environment, so when you call one of these values from your code, the environmentSettings engine tries to load the specific value based on the CurrentEnvironment parameter on your appSettings
Inside any of your environmentSections you can add your parameters directly as a KeyValue like Development or QA section.
<EnvironmentSettings>
<Production file = "Config\Production.config" ></ Production >
<Development>
<add key = "TestKey" value="DevValue"/>
<add key = "connection" value="MovieDBContextDev"/>
</Development>
<QA>
<add key = "TestKey" value="QAValue"/>
<add key = "connection" value="MovieDBContextQA"/>
</QA>
</EnvironmentSettings>
You can also use an external file in order to keep your config files isolated like Production section 1.
<Production>
<add key="TestKey" value="ProductionValue"/>
<add key="connection" value="MovieDBContextPrd"/>
</Production>
Once you have setup your envirnmentSettings, you must specify the current environment otherwise you will give an error
<appSettings>
<add key = "CurrentEnvironment" value="Development"/>
</appSettings>
So, you can call any of your values from your application code
string valueFromAppConfig = SecureEnvironmentSettings.Settings["TestKey"].Value;
Console.WriteLine($"CurrentValue: {valueFromAppConfig}");
/// CurrentValue: DevValue
ConnectionStrings
You can specify your all your connections strings, you need first, add your connectionString on that section, name it, and then add that name in your specific environmentSection, so you can call it from the SecureEnvironmentSettings library
<connectionStrings>
<add name = "MovieDBContextPrd" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesPrd.mdf" providerName="System.Data.SqlClient">
<add name = "MovieDBContextDev" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesDev.mdf" providerName="System.Data.SqlClient">
<add name = "MovieDBContextQA" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesQA.mdf"providerName="System.Data.SqlClient">
</connectionStrings>
<Development>
<add key = "connection" value="MovieDBContextDev"/>
</Development>
string connection = SecureEnvironmentSettings.GetConnectionString("MovieDBContextDev");
Console.WriteLine($"ConnectionValue: {connection}");
/// ConnectionValue: Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MoviesDev.mdf" providerName="System.Data.SqlClient
Footnotes
-
For more information [Using External Config Files in .NET Applications] (https://www.codeproject.com/Articles/1075487/Using-External-Config-Files-in-NET-Applications). β©