尝试从 web.config 打开一个部分会出现 ConfigurationErrorsException:条目 KEY 已被添加

发布于 2024-08-03 22:35:08 字数 1137 浏览 3 评论 0原文

我正在尝试从我的 asp.net 应用程序中的会员提供程序获取一些设置(这样我就不必重复 appSettings 部分中的信息),但出现以下异常:

System.Configuration.ConfigurationErrorsException: The entry 'MyMembershipProvider' has already been added. (C:\Inetpub\intranet-webapp\web.config line 53)

的代码如下:

var configurationManager = WebConfigurationManager.OpenWebConfiguration("/", "IntranetWebapp");
var section = (MembershipSection) configurationManager.GetSection("system.web/membership");

我 第二行引发异常。我觉得这很奇怪,因为我没有尝试向配置文件添加任何新信息。相反,我只是想找回它。

我的配置部分的内容:

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

非常感谢任何帮助。

巴勃罗

I'm trying to fetch some settings from my membership provider in my asp.net application (so that I don't have to duplicate the 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 the following:

var configurationManager = WebConfigurationManager.OpenWebConfiguration("/", "IntranetWebapp");
var section = (MembershipSection) configurationManager.GetSection("system.web/membership");

The exception is raised in the second line. I find it completely bizarre, since I am not trying to add any new information to the config file. Instead I'm just trying to retrieve it.

The contents 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 much appreciated.

Pablo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如此安好 2024-08-10 22:35:09

我能够通过在相关线程中执行 CzechDeveloper 所做的 操作来解决此问题:

<clear />

重要:只有当我将它直接放在复制的嵌套元素上方时,这才对我有用。

<sso>
  <stores>
    <clear />
    <store name="Store1" apiKey="abcd" />
    <store name="Store2" apiKey="efgh" />
  </stores>
</sso>

当我们开始将虚拟目录添加为therealsix也开始看到这些问题em> 指出。

I was able to fix this problem by doing what CzechDeveloper did, in a related thread, with:

<clear />

Important: This only worked for me when I put it directly above the nested element that was duplicating.

<sso>
  <stores>
    <clear />
    <store name="Store1" apiKey="abcd" />
    <store name="Store2" apiKey="efgh" />
  </stores>
</sso>

We also started seeing these problems when we started adding virtual directories as therealsix points out.

命硬 2024-08-10 22:35:09

假设我的评论已修复,我看不到您的代码有任何问题 - 使用完全相同的东西(显然站点属性的值不同)会产生一个填充的节变量,正如我所期望的那样。

我唯一能想到的是,您要么使用 ConfigurationFile.Getsection 执行其他操作,即尝试将数据合并到您的主要设置中,要么使用 OpenWebConfiguration 的重载来获取您已成功打开的站点将 web.config 从层次结构中的不同区域添加到您的站点中 - 您是否在 IIS/.NET 中定义了多个应用程序,这些应用程序的成员资格设置值会发生冲突?

最后,您的 DefaultProvider 值不应该读取“MyIntranetMembershipProvider”(您的提供商的名称),而不是“IntranetApp”(应用程序的名称(在提供商的数据存储中))。

Assuming the fix from my comment, I can't see anything wrong with your code - using the exact same thing (with a different value for the site property obviously) results in a populated section variable as I'd expect.

The only thing I can think of is that you're either doing something else with ConfigurationFile.Getsection, that is attempting to merge the data into your main setting, or by using the overload of OpenWebConfiguration that takes a site you've managed to open a web.config from a different area in the hierarchy into your site - do you have either multiple applications defined in IIS/.NET that would have conflicting values for their Membership setting?

Finally, shouldn't you DefaultProvider value read "MyIntranetMembershipProvider", which is the name of your provider, rather than "IntranetApp" which is the name of the application (in the provider's datastore).

忆伤 2024-08-10 22:35:09

将我的站点复制到同一站点下的虚拟目录后,我刚刚遇到了同样的错误。我想那里有一些继承。

我可以通过在有问题的行之前添加以下内容来解决这个问题:

<remove name="MyMembershipProvider"/>

I just got this same error after copying my site to a virtual directory under the same site. I guess there is some inheritance there.

I was able to get around it by adding this right before the line in question:

<remove name="MyMembershipProvider"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文