views:

380

answers:

3

Hi, I'm using .NET 3.5 / C# and the problem I face is that I need to be able to encrypt and decrypt a .config file which holds all my database connections to be used with the EnterpriseLibrary.Data.DatabaseProviderFactory, shown at end of question. This encryption and decryption is custom and is done with a known key and has to be the same for every machine hence the reason why i'm not using ProtectSection.

I'm trying to read an encrypted configurationSetings.config file, decrypt the file in memory and pass the decrypted content of that file to the DatabaseProviderFactory so that it is able to access the database connection settings.

I'm currently (non encrypted config file) using the MS Enterprise Libraries (v2) DatabaseProviderFactory in conjunstion with the FileConfigurationSource class to look at the App.config file for the name of the ConnectionSettings.config file, in which I store database connection settings.

I then use pass the path of the ConnectionSettings file to the FileConfigurationSource and then I pass the result to the DatabaseProviderFactory which allows me to create a database instance.

I don't want to have to write the unencrypted file to the filesystem and reading it back. I was looking that the option of using isolated storage for this but as I understand it you are unable to get a direct path to a file created using Isolated Storage.

Perhaps there's a way to create a custom ConfigurationSource or something that will allow me to pass the decrypted into directly into the DatabaseProviderFactory in order to create a database instance.

So in essence I need to read an encrypted configurationSetings.config file, decrypt the file in memory and pass the decrypted content of that file to the DatabaseProviderFactory so that it is able to access the database connection settings, or something similar so that I can map to Microsoft.Practices.EnterpriseLibrary.Data.Database.

Any code and/or advice would be much appreciated.

Thanks in Advance.

Dave

APP.CONFIG

  <enterpriseLibrary.ConfigurationSource selectedSource="fileSource">
    <sources>
      <add
        name="fileSource"
        type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b2b86dc1561d89b"
        filePath ="ConnectionSettings.config" />
      <add
        name="systemSource"
        type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b8b89dc1061d43b" />
    </sources>
  </enterpriseLibrary.ConfigurationSource>

METHOD CODE:

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource fileConfig =
   new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(connectionSettingsConfigFile);

Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory dbProvider =
   new Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory(fileConfig);

if (string.IsNullOrEmpty(databaseConnectionName))
{
   database = dbProvider.CreateDefault();
}
else
{
   database = dbProvider.Create(databaseConnectionName);
}

CONNECTIONSETTINGS.CONFIG

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5b2b86dc1561d89b" />
  </configSections>

  <dataConfiguration defaultDatabase="defaultConnection" />
  <connectionStrings>
    <add name="defaultConnection" connectionString="Data Source=192.11.111.11;Initial Catalog=TestDB;User Id:Test;Password:test"
        providerName="System.Data.SqlClient" />

  </connectionStrings>
</configuration>
A: 

It seems that instead of using the DatabaseProviderFactory, if it's changed to use a specific implementation such as SqlDatabase it allows you to pass in the connectionString as part of its constructor, which allows a bit more flexible. So since i'm now using SqlDatabase what i need to figure out now is how to pass the contents of the decrypted config file, as a string, into a Configuration object. Any Ideas?

Dave
A: 

I ended up created a custom configuration file that mimics that uses the base of the ConfigurationSettings class to read the .config file, so in effect I removed the Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings configuration section all together and replaced it with a custom configuration class.

Hope this helps someone.

Dave
A: 

Hi Dave, I am Facing the same problem, while encrypting the connection string in the config file.So can you plz provide the details / implementation of the custom configuration class u created.

shinie