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, eg "LDAP://ActiveDirectoryDomain2.com"
In C# code behind how do change the connectionString value?
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!
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.
When using settings like this, changing the value at runtime is easy:
Properties.Settings.Default.LdapConnectionString = "New Connection String";
Properties.Settings.Default.Save();
Basically, you need to forget about the web.config, and structure your code to use a connection string the exist only in memory.
var settings = ConfigurationManager.ConnectionStrings[ 0 ];
var fi = typeof( ConfigurationElement ).GetField( "_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic );
fi.SetValue(settings, false);
settings.ConnectionString = "Data Source=Something";
即使从应用程序内部修改web.config
文件也是一个坏主意,您可以尝试这样做:
System.Configuration.ConfigurationManager.AppSettings.Set("keyToBeReplaced", "newKeyValue");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.