【问题标题】:Create Virtual Directory and Set Permissions IIS7 - Cannot read configuration file due to insufficient permissions创建虚拟目录并设置权限 IIS7 - 由于权限不足而无法读取配置文件
【发布时间】:2010-03-19 21:55:52
【问题描述】:

我正在尝试创建一个虚拟目录并使用 IIS7 和 C# 设置它的权限。这是我的代码示例:

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

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

这会引发异常,消息是:

Cannot read configuration file due to insufficient permissions.

有人可以帮忙吗?

编辑

以管理权限运行给了我一个新错误:“启用读取配置文件”有人能告诉我它应该读取哪个配置以及如何访问它吗?

【问题讨论】:

  • 您在使用 Window Vista 吗?好吧,您可能必须从控制面板禁用 UAC。

标签: iis-7 virtual-directory configurationsection


【解决方案1】:

这听起来像是您的应用程序没有正确的权限。为了能够操作 IIS7 的配置,运行您的应用程序的帐户必须是管理员帐户,或者是受信任的计算库可识别的帐户,例如 SYSTEM 帐户。

如果您在 Visual Studio 中进行调试,请务必使用资源管理器中的“以管理员身份运行”功能或从快速启动工具栏中启动 Visual Studio。

【讨论】:

    【解决方案2】:

    我错误地使用了 ServerManager obj。它没想到构造函数中的网站名称。以下是在IIS7中设置匿名访问的正确方法。

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

    【讨论】: