Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change the ConnectionString configuration value at runtime? Or... do I even need to?

Tags:

c#

asp.net-4.0

First post, I'm a complete .Net/C# novice thrown in at the deep end!

I've inherited a C# wed application due to someone leaving at work and me being the only one with bandwidth! But not the .Net, C# knowledge!

The app is used by people on different sites all over the world. They log in using the corporate login details and as such they log into different servers depending on where they are located, (Europe, America or India).

The guy who wrote the app couldn't work out how to switch the ConnectionString in web.config depending on location, so duplicated the whole app for each domain! With the only variation being a single IP address in web.config for each duplicated version of the app! Then did a simple web front page which took the user to "their" version of the app depending on where they said they were in the world!

The first thing I want to do is to move to a single version to maintain, so I need to be able to switch the connection string or how to login?

I've spent several days trying to work out how I get to ConnectionString (defined in web.config) from my Login class, only to discover the values set in web.config seem to be read only, so I can't alter them.

So I guess the first question is, am I barking up the wrong tree? Can I just set all the information that AspNetActiveDirectoryMembershipProvider (see code later) requires and call it from my login class? Or is the ConnectionString route the Ipso facto way to set up connections in .Net/C#? So therefor I do need to find out how to change/specify/add the value at runtime.

Three possibilities I can think of:- (The first is the one I've ground to a hult with)

  1. Change the ConnectionString for ADService in my web.config from my Login class?

  2. Change what AspNetActiveDirectoryMembershipProvider uses, so from my Login class magicly get it to use EMEA_ADService or PACIFIC_ADService as defined in web.config?

  3. Is it possible to define a new connectionString and call AspNetActiveDirectoryMembershipProvider all from my Login class, not using web.config for this connection at all?

Here's a bit of my/his web.config file and my Login class

Cuttings from Web.config

<connectionStrings>
    <add name="ADService" connectionString="LDAP://12.345.67.8" />          *---- Original ConnectionString (IP address changed)----* 
    <add name="EMEA_ADService" connectionString="LDAP://12.345.67.8" />     *---- Added by me playing around unsuccessfully! ----* 
    <add name="PACIFIC_ADService" connectionString="LDAP://12.345.67.9" />  *---- Added by me playing around unsuccessfully! ----* 
    ~
  </connectionStrings>

<authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" timeout="2880" />     *---- The background class for this popup (Login.aspx.cs) is where I'm currently trying to affect ConnectionString----* 
    </authentication>
                                                           *---- Pretty sure this is the bit that actually does the login verification----* 
    <membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
      <providers>
        <clear />
        <add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider,            System.Web, Version=4.0.0.0, Culture=neutral,            PublicKeyToken=12345678" connectionStringName="ADService" applicationName="/." description="ADService" />
      </providers>
    </membership>

This is as far as I've got in my class before finding out that I don't appear to be able to alter ConnectionString!

Cuttings from Login.aspx.cs

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings; //this is now working :)
        string userDomain = Environment.UserDomainName;  //Probably don't need this, it seems to give the login domain on the machine. Don't know yet if that will be the users machine or the server the app runs on?
        if (connections.Count != 0)
        {
            foreach (ConnectionStringSettings connection in connections)
            {
                string testname = connections["ADService"].Name;
                string testConnectionString = connections["ADService"].ConnectionString;
                connections["ADService"].ConnectionString = "LDAP://12.345.67.9";
                testConnectionString = connections["ADService"].ConnectionString;

Any hint would be very welcome!

P.S. I have requested a .Net/C# course at work! ;)

like image 640
user2008415 Avatar asked Jan 24 '13 18:01

user2008415


1 Answers

You wouldn't want to alter the existing connection string. Rather, you'd want to alter which connection string your Data Access Layer was using to call different service stacks. You could then choose a connection string at runtime based on whatever input parameters you wanted to use. which in your case might be an IP range.

asp.net mvc multiple connection strings

Handling multiple connection strings in ONE DataAccess Layer

http://msdn.microsoft.com/en-us/library/aa479086.aspx

The microsoft article is particularly interesting since it actually takes an architectural look at proper patterns for resolving dilemmas like yours. I think you got stuck with the short end of the stick! Best of luck!

like image 176
David L Avatar answered Sep 29 '22 21:09

David L