user1593455
user1593455

Reputation: 71

I cannot set IIS authentication programatically using c#

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?

Upvotes: 5

Views: 1814

Answers (1)

Nicolas R
Nicolas R

Reputation: 14619

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

Upvotes: 2

Related Questions