Modify ISAPI and CGI extensions

夙愿已清 提交于 2019-12-23 12:09:17

问题


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.

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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/15335635/modify-isapi-and-cgi-extensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!