5

我正在尝试使用 c# 在站点上打开匿名身份验证

这段代码抛出了写在底部的异常我到处搜索我找不到答案如何解决它:

using (ServerManager serverManager = new ServerManager())
{
    Configuration configapp = serverManager.GetApplicationHostConfiguration();
    ConfigurationSection anonymousAuthenticationSection = configapp.GetSection("system.webServer/security/authentication/anonymousAuthentication", Site1);
    anonymousAuthenticationSection["enabled"] = false;

    serverManager.CommitChanges();
} 

例外:

System.IO.FileNotFoundException: Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config
Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/Site1'
   at Microsoft.Web.Administration.Interop.AppHostWritableAdminManager.GetAdminSection(String bstrSectionName, String bstrSectionPath)
   at Microsoft.Web.Administration.Configuration.GetSectionInternal(ConfigurationSection section, String sectionPath, String locationPath)
   at Microsoft.Web.Administration.Configuration.GetSection(String sectionPath, String locationPath)
   at FlowSuiteWebConfigurator.label.button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\Projectname\Projectname\Form1.cs:line 264

有人可以指导我如何解决这个问题吗?

4

1 回答 1

2

可能有点晚了(一年半之后......),但问题似乎出在您的位置定义中(configapp.GetSection 的第二个参数):异常说Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/Site1'

在我的机器上进行本地测试,我也不例外,使用有效的位置名称,如Default Web Site/MyCustomPortal.

注意:添加的参考Microsoft.Web.Administration.dll在 下C:\Windows\System32\inetsrv\,这些代码行查看applicationHost.config位于的文件C:\Windows\System32\inetsrv\config

于 2013-12-26T11:32:40.870 回答