Print Page | Close Window

Remove password from Connection String

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2313
Printed Date: 29-Jul-2026 at 8:55am


Topic: Remove password from Connection String
Posted By: ptit_bab
Subject: Remove password from Connection String
Date Posted: 17-Nov-2010 at 2:48am
Hi all,
Here is my configuration file. It contains the connection string used by EF4 and Devforce 2010 :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="testEntities1" 
         connectionString=
         "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
          Data Source=.\MYINSTANCE;
          Initial Catalog=test;
          User ID=SPONGE;
Password=BOB;
          MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

This is clearly not a secure way to connect to the Database...

I want to at least remove the password from the Connection String and specify it within application code.
Like this :

<configuration>
  <connectionStrings>
    <add name="testEntities1" 
         connectionString=
         "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
          Data Source=.\MYINSTANCE;
          Initial Catalog=test;
          User ID=SPONGE;
Password=BOB;
          MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

How can I do this ?

When I disable Devforce and use only Entity Framework 4.0, I can do that with the following piece of code :

// Instanciate Entity Manager

testEntities1 context = new testEntities1();

// Get connection string and set some parameters
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(((EntityConnection)context.Connection).StoreConnection.ConnectionString);
sb.IntegratedSecurity = false;
sb.UserID = "SPONGE";
sb.Password = "BOB";

// Set new connection string to EF
((EntityConnection)context.Connection).StoreConnection.ConnectionString = sb.ConnectionString;

I need to do something like this with Devforce.

Thanks a lot for your help ;)






Replies:
Posted By: kimj
Date Posted: 17-Nov-2010 at 9:48am

In DevForce you can do this with an implementation of the IDataSourceKeyResolver interface.  The interface supports a single method, GetKey, which DevForce will call the first time it needs to work with the data source.  The "key" returned by the method is usually a ClientEdmKey, which is basically just strongly-typed connection information.  In your GetKey method you can build up the connection string entirely from code, or take an existing connection string from the config and modify it as needed, or as you do above, load it into a SqlConnectionStringBuilder to work with.

Here's an example:
 
[System.Runtime.Serialization.DataContract]
public class DynamicDataSourceKeyResolver : IDataSourceKeyResolver {

  public IDataSourceKey GetKey(String keyName, String keyExtension, bool onServer) {
    if (!onServer) return null;

    EntityConnectionStringBuilder eBuilder = new EntityConnectionStringBuilder(cs.ConnectionString);
    SqlConnectionStringBuilder sBuilder = new SqlConnectionStringBuilder(eBuilder.ProviderConnectionString);
    sBuilder.Password = "bob";
    eBuilder.ProviderConnectionString = sBuilder.ConnectionString;
    return new ClientEdmKey(keyName, eBuilder.ConnectionString);
  }
}
 


Posted By: ptit_bab
Date Posted: 18-Nov-2010 at 12:27am
Hello Kimj,

thank you for your answer. The implementation of IDataSourceKeyResolver resolve my issue.

Thank you again for your help :)



Print Page | Close Window