Question

I have a problem with IIS server,

How can I modify ISAPI elements with using C# language?

Forexample : ASP.net V4.0 restriction is "Not Allowed". And I want to set as "Allowed" like below picture.

enter image description here

I can add an elements with this Code. But I cant modify.

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection isapiFiltersSection = config.GetSection("system.webServer/isapiFilters");
         ConfigurationElementCollection isapiFiltersCollection = isapiFiltersSection.GetCollection();

         ConfigurationElement filterElement = isapiFiltersCollection.CreateElement("filter");
         filterElement["name"] = @"SalesQueryIsapi";
         filterElement["path"] = @"c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll";
         filterElement["enabled"] = true;
         filterElement["enableCache"] = true;
         isapiFiltersCollection.Add(filterElement);

         serverManager.CommitChanges();
      }
   }
}

Thanks For your advice.

Was it helpful?

Solution

I found a solution. I changed the code like below. and it worked.

private void buttonOK_Click(object sender, EventArgs e)
   {

        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();
            ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
            ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
            foreach (ConfigurationElement element in isapiCgiRestrictionCollection)
            {
                element.SetAttributeValue("allowed", false);
            }

            ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");

            serverManager.CommitChanges();          
        }
    }

enter image description here

OTHER TIPS

If you want to add a restriction, you can do it with this code:

public static void AddIsapiRestriction(string name, string path)
{
    using (var serverManager = new ServerManager())
    {
        var config = serverManager.GetApplicationHostConfiguration();
        var isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
        var isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
        if (isapiCgiRestrictionCollection.ToList().Exists(x => x.GetAttribute("path").Value.ToString() == path))
            return;
        var addElement = isapiCgiRestrictionCollection.CreateElement("add");
        addElement["description"] = name;
        addElement["path"] = path;
        addElement["allowed"] = true;
        isapiCgiRestrictionCollection.Add(addElement);
        serverManager.CommitChanges();              
    }            
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top