2010-03-19 256 views
1

我想创建一个虚拟目录并使用IIS7和C#设置它的权限。这里是我的代码示例:创建虚拟目录并设置权限IIS7 - 由于权限不足,无法读取配置文件

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.

有人能帮忙吗?

编辑

与管理权限运行给了我一个新的错误:“启用读取配置文件”谁能告诉我这配置应该读,我怎么能访问它?

+0

你在使用Window Vista吗?很可能你必须从控制面板禁用UAC。 – wonde 2010-03-20 04:11:21

+0

Server 2008 64。 – Nick 2010-03-22 22:36:07

回答

0

我错误地使用了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; 
0

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

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