Trying to open a section from web.config catches a ConfigurationErrorsException: KEY has already been added
I am trying to extract some parameters from my membership provider in my asp.net application (so I don't need to duplicate information in the appSettings section), but I am getting the following exception:
System.Configuration.ConfigurationErrorsException: The entry 'MyMembershipProvider' has already been added. (C:\Inetpub\intranet-webapp\web.config line 53)
My code is as follows:
var configurationManager = WebConfigurationManager.OpenWebConfiguration("/", "IntranetWebapp");
var section = (MembershipSection) configurationManager.GetSection("system.web/membership");
The exception is thrown on the second line. I find this completely quirky since I am not trying to add any new information to the config file. Instead, I am just trying to restore it.
Content of my config section:
<membership defaultProvider="IntranetApp">
<providers>
<add applicationName="IntranetApp"
enableSearchMethods="true"
connectionStringName="IntranetConnectionString"
connectionUsername="CN=username,OU=Service Accounts,OU=Users,OU=CompanyName,DC=CompanyName,DC=com"
connectionPassword="********"
name="MyIntranetMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider" />
</providers>
</membership>
Any help is greatly appreciated.
Pablo
source to share
Assuming the fix from my comment, I don't see anything wrong with your code - using the exact same thing (with a different value for the site property explicitly) results in the filled section variable as I expected.
The only thing I can think of is that you are either doing something else with ConfigurationFile.Getsection, which tries to merge the data into your main parameter, or using the OpenWebConfiguration overload that takes up the site you are using managed to open the web. config file from a different area in the hierarchy to your site - do you have multiple applications defined in IIS / .NET that have conflicting values โโfor their membership parameter?
Finally, shouldn't you read the DefaultProvider value "MyIntranetMembershipProvider", which is the name of your provider, and not "IntranetApp", which is the name of the application (in the provider's data store).
source to share
I was able to resolve this issue by executing CzechDeveloper on the linked thread with
<clear />
Important: This only worked for me when I placed it directly above the nested element that I was duplicating.
<sso>
<stores>
<clear />
<store name="Store1" apiKey="abcd" />
<store name="Store2" apiKey="efgh" />
</stores>
</sso>
We also started seeing these issues when we started adding virtual directories as atalsix points out.
source to share