New Posts New Posts RSS Feed: Remove password from Connection String
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Remove password from Connection String

 Post Reply Post Reply
Author
ptit_bab View Drop Down
Newbie
Newbie
Avatar

Joined: 16-Nov-2010
Location: Obernai
Posts: 2
Post Options Post Options   Quote ptit_bab Quote  Post ReplyReply Direct Link To This Post Topic: Remove password from Connection String
    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 ;)



Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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);
  }
}
 


Edited by kimj - 17-Nov-2010 at 10:36am
Back to Top
ptit_bab View Drop Down
Newbie
Newbie
Avatar

Joined: 16-Nov-2010
Location: Obernai
Posts: 2
Post Options Post Options   Quote ptit_bab Quote  Post ReplyReply Direct Link To This Post 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 :)
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down