문제

I want to add a handler mapping (script map) for our custom CGI-Exe to an Internet Information Server using the classes in the Microsoft.Web.Administration namespace. The (simplified) code is:

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();

This code adds the handler mapping to the mapping list in the IIS, but it is marked as disabled: screenshot of the disabled handler mapping in the IIS manager

I have to manually select “Edit Feature Permissions…” from the Actions pane and select the “Execute” permission in the following dialog:

screenshot of the Edit Feature Permissions dialog

I want to know how to enable the handler mapping, either by setting different configuration options while creating the handler or by programmatically editing the feature permissions.

Update

I copied the web.config that was created as a result of this script, then I manually added the Execute permission with the dialog above and compared the resulting web.config to the original one:

The start changed from:

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

to

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

Now I need to find out, how to set the accessPolicy. But why is this set on the handlers node and not on my specific handler node?

도움이 되었습니까?

해결책

Sometimes it helps just to formulate the question on stackoverflow. After playing with this for two days, I found the solution just hours after posting the question:

I needed to set the accessPolicy for the handlers.

After adding this line it finally worked:

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

It seems I just used the wrong search terms, I looked for "edit feature permissions" instead of accessPolicy.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top