Reputation: 2803
In need to create an small console app, that takes two arguments:
app.config
file.connectionStrings
and Encrypt it and then save the encrypted text to the config file. I have looked, but have not found any solution for it. My app.config
file could look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="Conn1" connectionString="Data Source=Database1;Initial Catalog=DB12;User ID=User1234;Password=Qwerty123" providerName="System.Data.SqlClient" />
<add name="Conn2" connectionString="Data Source=Database2;Initial Catalog=DB12;User ID=User1234;Password=Qwerty123" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
I have the encryption part are done. I just need to in this example to read those two connectionstrings
and encrypt them each. Please note that the name of the connectStrings
are different every time, since the console app are triggered by TFS build server.
Upvotes: 4
Views: 4962
Reputation: 3775
came up with this to work with both app and web, note i did also need to change the values and save them to disk so couldn't just loop through
var appConfig = System.Web.HttpContext.Current == null
? ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
: System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var needsToSave = false;
foreach (var appCon in appConfig.ConnectionStrings.ConnectionStrings.Cast<ConnectionStringSettings>())
{
//do stuff
}
Upvotes: 0
Reputation: 573
If you don´t know the name of the connection string, you could try it like this:
ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
if (connections.Count != 0)
{
//go trough all available ConnectionStrings in app.config
foreach (ConnectionStringSettings connection in connections)
{
//reading the ConnectionString
string conString = ConfigurationManager.ConnectionStrings[connection.Name].ConnectionString;
//writing the ConnectionString
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings[connection.Name].ConnectionString = EncryptConfig(conString); //just call your Encryption part here instead of "EncryptConfig()"
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
}
}
Upvotes: 1
Reputation: 186
Basically you don't really need the name of the Connection String but the connection itself. I believe you just need to go thru them with foreach
and for each iteration you can get the connectionString till the last one in the app.config.
foreach (System.Configuration.ConnectionStringSettings css in System.Configuration.ConfigurationManager.ConnectionStrings)
{
string connectionString = css.ConnectionString;
// encryption part
// rewriting the connectionString in the app.config or however you want ot be done
}
You mentioned you got the encryption part done and all you need is reading the strings.
Hope it helps!
Upvotes: 5