Question

I'm trying to enable anonymous access on an application in IIS7, using the code below:

ConfigurationSection config = server.GetWebConfiguration(webSiteName).GetSection("system.webServer/security/authentication/anonymousAuthentication", "/" + applicationName);
config.OverrideMode = OverrideMode.Allow;
config["enabled"] = true;

However I'm getting this error:

Failed: The request is not supported. (Exception from HRESULT: 0x80070032)

How can I modify anonymous access for the application?

Thanks, ng93

Was it helpful?

Solution

The code above is invalid since the section is locked at ApplicationHost.config level for security reasons. In the code you are trying to use it is trying to set it in Web.config. If you really wanted that you would first need to request it from a GetApplicationHost call set the overrideMode and then get the section again from a GetWebConfiguration. But all in all I would still recommend instead setting that value at the server level so that it cannot be accidentally changed in web.config by a deployment or some other mechanism.

So to do that I would instead recommend doing:

string webSiteName = "Default Web Site";
string applicationName = "MyApp";

using (ServerManager server = new ServerManager())
{
    ConfigurationSection config = server.GetApplicationHostConfiguration()
                                        .GetSection(@"system.webServer/security/
                                                     authentication/
                                                     anonymousAuthentication", 
                                                     webSiteName + "/" + applicationName);
    config.OverrideMode = OverrideMode.Allow;
    config["enabled"] = true;
    server.CommitChanges();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top