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

2

7

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! ;)

Egger answered 24/1, 2013 at 18:1 Comment(2)
I am so sorry. Best of luck.Gist
You can assemble a name on the fly, so if you can determine where you are, e.g. "Pacific", then you can use Connections[location + '_ADService'] to get the right connection string.Jemina
T
5

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!

Trela answered 24/1, 2013 at 18:8 Comment(2)
Thank you for the help. I guess the answer is though that I need to go away an learn a few things about .NET and C# first! :) So the change will have to go back on the backlog until I have at least a vague idea about how to do, what I want to do, in this environment! lolEgger
@Egger You are welcome! You're dealing with some challenging stuff right off the bat and I hope you find all the answers you're looking for! Best of luck and thank you for accepting my answer.Trela
U
0

The Web.config cannot be modified at Runtime. I would suggest setting some kind of flag via a login link or combobox on the website for people to use to choose where they want to login. It is not the job of the server to figure out what a user wants to do.

Unshaped answered 24/1, 2013 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.