Create Virtual Directory and Set Permissions IIS7 - Cannot read configuration file due to insufficient permissions

二次信任 提交于 2020-01-06 02:19:28

问题


I am trying to create a virtual directory and set it's permissions using IIS7 and C#. Here is a sample of my code:

using (ServerManager serverManager = new ServerManager(webSite))
{
    ConfigurationSection anonymousAuthenticationSection =
      config.GetSection(
          @"system.webServer/security/authentication/anonymousAuthentication",
          webSite);
    anonymousAuthenticationSection["enabled"] = true;

    serverManager.CommitChanges();
    return "true";
}

This throws an exception and the message is:

Cannot read configuration file due to insufficient permissions.

Can someone help?

EDIT

Running with administration privileges gives me a new error: "Enable to read configuration file" Can someone tell me which config it should be reading and how I can access it?


回答1:


This sounds like your application doesn't have the correct permissions. To be able to manipulate IIS7's configuration the account your application runs under must be an Administrator account, or an account that is recognised by the trusted computing base such as the SYSTEM account.

If you're debugging this in Visual Studio, be sure to launch Visual Studio using the "Run as Administrator" feature in explorer or from the quicklaunch toolbar.




回答2:


I was using the ServerManager obj incorrectly. It was not expecting the website name in the constructor. The following is the correct way to set anonymous access in IIS7.

using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    ConfigurationSection anonymouseAuthenticationSetion =                                            config.GetSection("system.webServer/security/authentication/anonymousAuthentication", webSite);
                    anonymouseAuthenticationSetion["enabled"] = true;


来源:https://stackoverflow.com/questions/2480827/create-virtual-directory-and-set-permissions-iis7-cannot-read-configuration-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!