views:

241

answers:

3

Hello,

I have the following in my web.config

<connectionStrings>
   <add name="ActiveDirectoryConnection" connectionString="LDAP://ActiveDirectoryDomain1.com" providerName="System.Web.Security.ActiveDirectoryMembershipProvider"/>
</connectionStrings>

I need to add a dropdown box to my login page that allows the user to change the connectionString to a different string, e.g. "LDAP://ActiveDirectoryDomain2.com"

In C# code behind how do change the connectionString value?


More info:

The problem I am having is that there are 4 other web.config settings call that one connectionString. For example:

<activeDirectorySecurityContextSettings connectionStringName="ActiveDirectoryConnection" defaultADUserName="ReportUser" defaultADPassword="password"/>  

Thanks!

+4  A: 

If a user is able to change the value of the Setting, then the web.config file is the wrong place to store the setting.

You should check out a User Scoped value in a Settings file instead.

MSDN - Using Settings in C#

When using settings like this, changing the value at runtime is easy:

Properties.Settings.Default.LdapConnectionString = "New Connection String";
Properties.Settings.Default.Save();
Justin Niessner
Or alternately, if you want it to be app-wide (I'm thinking like an app administrator may want to make changes for the application database for all users, and not a per-connection change) then you could use a singleton class for all your connection strings, and change them in the singleton class from your app interface... This data could then be persisted elsewhere, and still be saveable between app-sessions.
drachenstern
@drachenstern - Actually, it would still be smarter to use Settings. You would just make it Application scope rather than User. Much easier than trying to implement yet another Singleton that the world doesn't need.
Justin Niessner
The problem I am having is that there are 4 other web.config settings call that one connectionString. For example: <activeDirectorySecurityContextSettings connectionStringName="ActiveDirectoryConnection" defaultADUserName="ReportUser" defaultADPassword="password"/> So it seems I cannot put it in the Settings file.
@Justin Niessner ~ Thanks, I see I need to go brush up on Settings, I thought it was user-based, not app as well ... /facepalm strikes again!
drachenstern
A: 
var settings = ConfigurationManager.ConnectionStrings[ 0 ];

var fi = typeof( ConfigurationElement ).GetField( "_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic );

fi.SetValue(settings, false);

settings.ConnectionString = "Data Source=Something";
Eton B.
A: 
  • It's a bad idea to modify a *.config file from inside the program.
  • It's a bad idea for a webpage to modify any file in the root folder of your website.
  • It's a bad idea to have permission set allowing a web page the modification of files in the root folder of your website.

Basically, you need to forget about the web.config, and structure your code to use a connection string the exist only in memory.

James Curran