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

First post, I'm a complete newbie .Net / C # thrown into the deep end!

I inherited the C # wed application because someone went to work and I was the only one with bandwidth! But not knowledge of .Net, C #!

The application is used by people on different sites around the world. They are registered using corporate login data and, as such, are registered on different servers depending on where they are located (Europe, America or India).

The guy who wrote the application was not able to decide how to switch ConnectionString to web.config depending on the location, so it duplicates the entire application for each domain! The only option is the single IP address in web.config for each duplicated version of the application! Then a simple web page appeared, on which the user β€œhad his” version of the application, depending on where they said they were in the world!

The first thing I want to do is upgrade to one version for support, so I need to be able to switch the connection string or how to log in?

I spent several days figuring out how I access ConnectionString (defined in web.config) from my Login class, only to find the values ​​set in web.config, it seems to be read-only, t change them.

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

Three possibilities that I can think of: - (The first is what I founded hult with)

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

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

  • Is it possible to define a new connectionString and call AspNetActiveDirectoryMembershipProvider all of my Login class without using web.config for this connection at all?

Here is a bit of my / his web.config file and my login class

Worms 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 I understand it in my class, before figuring out that I cannot change the ConnectionString!

Worms 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!

PS I requested the .Net / C # course at work !;)

+7
source share
2 answers

You do not want to modify the existing connection string. Most likely, you will need to change which connection string your data access level uses to invoke different service stacks. You can then select a connection string at runtime based on any input parameters that you wanted to use. which in your case can be an IP range.

multiple asp.net mvc 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 in that it really takes an architectural look at the right patterns to solve dilemmas like yours. I think you're stuck with the short end of a stick! Good luck

+5
source

Web.config cannot be changed at runtime. I would suggest setting some kind of flag through the login link or combo box on the website so that people can choose where they want to enter. It is not the server’s task to understand what the user wants to do.

0
source

All Articles