5

I'm trying to turn of Anonymous authentication on a site using c#

This code throws exception written at bottom i searched everywhere i cant find answer how to solve it:

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

    serverManager.CommitChanges();
} 

The exception:

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

Can someone guide me how to fix this?

2
  • Seeing as you have C# tagged, I'm guessing this is ASP.NET. If you are wanting to deny any users that aren't authenticated, you would be better off doing it in the web.config of your actual website. This should deny unauthorized users from accessing your site. <system.web><authorization><deny users="?"/></authorization></system.web> Commented Aug 30, 2012 at 18:48
  • I'm actually using c# as IIS web configurator form. and i want first to disable anon login programaticaly and then apply only "windows authentification" Commented Aug 31, 2012 at 7:20

1 Answer 1

2

A bit late maybe (1 year and a half after...), but the problem seems to be in your location definition (2nd parameter of configapp.GetSection): the exception said Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/Site1'

Tested locally on my machine, I have no exception with a valid location name like Default Web Site/MyCustomPortal.

Note: the reference added is Microsoft.Web.Administration.dll under C:\Windows\System32\inetsrv\ , and those lines of code look into the file applicationHost.config located in C:\Windows\System32\inetsrv\config

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.