Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot set IIS authentication programatically using c#

Tags:

c#

iis-7

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?

like image 428
user1593455 Avatar asked Nov 03 '22 17:11

user1593455


1 Answers

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

like image 176
Nicolas R Avatar answered Nov 08 '22 09:11

Nicolas R