I cannot set IIS authentication programmatically using C #

I am trying to enable anonymous authentication on a site using C #

This code throws an exception, written below, I searched wherever I can find the 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();
} 

An 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 anyone advise me how to fix this?

+5
source share
1 answer

A little late (a year and a half after ...), but the problem seems to be in the location (the second parameter is configapp.GetSection): an exception is indicated Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/Site1'

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

: Microsoft.Web.Administration.dll C:\Windows\System32\inetsrv\, applicationHost.config, C:\Windows\System32\inetsrv\config

+2

All Articles