2012-08-30 32 views
5

我試圖使用C#的網站上開啓匿名身份驗證的我無法設置IIS認證編程方式使用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 

有人能指導我如何解決這一問題?

+0

看到你有C#的標籤,我猜這是ASP.NET文件applicationHost.config。如果您想拒絕任何未通過身份驗證的用戶,最好在實際網站的web.config中執行此操作。這應該拒絕未經授權的用戶訪問您的網站。 seekerOfKnowledge

+0

我實際上使用c#作爲IIS web配置器的形式。我想首先禁用匿名登錄programaticaly,然後只應用「Windows認證」 – user1593455

回答

2

有點晚了,也許(1年半後...),但問題似乎是在你的位置定義(configapp.GetSection的第二個參數):除了說Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/Site1'

測試在本地我的機器,我有一個有效的位置名稱例如Default Web Site/MyCustomPortal

注:添加的引用是C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll,並且這些代碼行看看位於C:\Windows\System32\inetsrv\config

相關問題