How to run a CGI program under IIS 7.0 or IIS 7.5

Looking around I didn’t find a good documentation on how to get good old CGI’s running on IIS 7 or 7.5. Here is a quick walkthrough:

1. Let’s write a quick CGI:    Take the following code and save it as simplecgi.cs in the directory c:inetpubwwwrootcgi

using System; using System.Collections;
class SimpleCGI {     static void Main(string[] args)     {         Console.WriteLine("rnrn");         Console.WriteLine("<h1>Environment Variables</h1>");         foreach (DictionaryEntry var in Environment.GetEnvironmentVariables())             Console.WriteLine("<hr><b>{0}</b>: {1}", var.Key, var.Value);     } }

2. Change into the C:inetpubwwwrootcgi directory and compile the source by using the following command-line:

%windir%Microsoft.NETFrameworkv2.0.50727csc.exe SimpleCGI.cs

You will have simplecgi.exe in the cgi directory. You can execute it on the command-line to see its output.

3. For security reasons every CGI has to be registered in the ISAPI/CGI Restriction list. To do that you have to open INETMGR, click the machine node (name of your machine) and find the ISAPI/CGI Restriction List menu icon.

image

Select the item and add the following entries in the dialog box:

image

Alternatively you can use the command-line:

%windir%system32inetsrvappcmd set config -section:isapiCgiRestriction  /+[path='c:inetpubwwwrootcgisimplecgi.exe',allowed='true',description='SimpleCGI']

4. The next step is to create a virtual directory that allows CGIs to execute. Right click the “Default Web Site” in INETMGR and select “Add Virtual Directory”. Add the following entries:

image

Here is the command-line which does the same:

appcmd add vdir /app.name:"Default Web Site/" /path:/cgi  /physicalPath:"c:inetpubwwwrootcgi"

5. One last step: we still don’t allow “Execute” access in this directory. For this you have to go to the Handler Mappings menu of the CGI virtual directory (make sure you select the CGI virtual directory on the left hand side!).

image

Go to the “Edit Feature Permissions” link in the Actions Menu on the right hand side, open it and check “Execute” and click “OK”.

image

via command-line:

appcmd set config "Default Web Site/cgi" /section:handlers  -accessPolicy:"Read,Script,Execute"

Now you are ready to go. Type “http://localhost/cgi/simplecgi.exe and you should see the following output:

image

You may also like