Tuesday, January 12, 2010

Enterprise Library 2.0 Encrypted ConnectionString

Enterprise Library 2.0 connection string is not encrypted by default. So recompile "Microsoft.Practices.EnterpriseLibrary.Data" DLL with following change...

In Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabaseAssembler decrypt "connectionStringSettings.ConnectionString" as you wish before calling SqlDatabase

public Database Assemble(string name, ConnectionStringSettings connectionStringSettings, IConfigurationSource configurationSource)
{
string conn = YourDecryptionLogic(connectionStringSettings.ConnectionString)

return new SqlDatabase(conn);
}

2 comments:

  1. This cool man...one more thing if you can post this link to the forums where users wants to do this...

    Cheers

    ReplyDelete
  2. Sure but meanwhile i figured out another easy way for this...call following at start of your code. You may change OpenExeConfiguration with OpenMappedExeConfiguration if using custom config files...

    string sectionName = "connectionstrings";
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection section = config.GetSection(sectionName);
    if (section != null)
    {
    if (!section.IsReadOnly())
    {
    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
    section.SectionInformation.ForceSave = true;
    config.Save(ConfigurationSaveMode.Modified);
    }
    }
    ConfigurationManager.RefreshSection(sectionName);

    ReplyDelete