为什么IIS ServerManager类会禁用我的处理程序映射?

时间:2012-10-18 11:36:47

标签: c# iis iis-7 servermanager

我想使用Microsoft.Web.Administration命名空间中的类将自定义CGI-Exe的处理程序映射(脚本映射)添加到Internet Information Server。 (简化)代码是:

var serverManager = ServerManager.OpenRemote(serverName);

Configuration webConfig = serverManager.GetWebConfiguration(siteName, appPath);
ConfigurationSection handlersSection = webConfig.GetSection("system.webServer/handlers");

ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();
string elementName = cgiFile + " script map appHost added by ACM";
ConfigurationElement addElement = handlersCollection.CreateElement("add");
addElement["allowPathInfo"] = true;
addElement["modules"] = "CgiModule";
addElement["name"] = elementName;
addElement["path"] = cgiFile;
addElement["requireAccess"] = "Execute";
addElement["scriptProcessor"] = Path.Combine(scriptsPath, cgiFile);
addElement["verb"] = "*";

handlersCollection.Add(addElement);
serverManager.CommitChanges();

此代码将处理程序映射添加到IIS中的映射列表,但它被标记为已禁用: screenshot of the disabled handler mapping in the IIS manager

我必须从“操作”窗格中手动选择“编辑功能权限...”,然后在以下对话框中选择“执行”权限:

screenshot of the Edit Feature Permissions dialog

我想知道如何启用处理程序映射,方法是在创建处理程序时设置不同的配置选项,或者通过编程方式编辑功能权限。

更新

我复制了由于此脚本而创建的web.config,然后我使用上面的对话框手动添加了Execute权限,并将生成的web.config与原始web.config进行了比较:

开始改为:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers accessPolicy="Read, Execute, Script">

现在我需要找出如何设置accessPolicy。但是为什么这个设置在处理程序节点而不是我的特定处理程序节点上?

1 个答案:

答案 0 :(得分:3)

有时它只能在stackoverflow上制定问题。在玩了两天之后,我在发布问题几个小时后找到了解决方案:

我需要为处理程序设置accessPolicy。

添加此行后,它最终有效:

handlersSection["accessPolicy"] = "Read, Script, Execute";

似乎我只是使用了错误的搜索字词,我查找了“编辑功能权限”而不是accessPolicy。