如何在 C# 中修改 IIS FTP 特定路径的授权规则?

How do I modify an IIS FTP Authorization Rule for a specific path in C#?

在 Server 2008+ 中,我以编程方式在 FTP 站点的虚拟目录 Reports 中创建新文件夹。我可以为每个新文件路径创建一个新的 FTP 授权规则:

using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection authorizationSection = config.GetSection("system.ftpServer/security/authorization", "FTP/LDNClient/Reports/aClientPath");
ConfigurationElementCollection authorizationCollection = authorizationSection.GetCollection();

ConfigurationElement addElement = authorizationCollection.CreateElement("add");
addElement["accessType"] = @"Allow";
addElement["users"] = @"LDNClient";
addElement["roles"] = @"";
addElement["permissions"] = @"Read, Write";
authorizationCollection.Add(addElement);

serverManager.CommitChanges();
}

其中 "FTP/LDNClient/Reports/aClientPath" 是规则的路径。但是对于同一个用户不同的路径有大量的元素。如果我打开 applicationHost.config,我可以看到不同的 ConfigurationElements,路径如下,"aClientPath":

<location path="FTP/LDNClient/Reports/aClientPath">
    <system.ftpServer>
        <security>
            <authorization>
                <remove users="LDNClient" roles="" permissions="Write" />
                <add accessType="Allow" users="LDNClient" permissions="Read, Write" />
            </authorization>
        </security>
    </system.ftpServer>
</location>

但我不知道如何引用那个元素,所以我可以 (1) 删除它或 (2) 修改权限。我可以通过以下方式遍历每个节点:

foreach (ConfigurationElement item in authorizationCollection)  
{
   // Do something with item here
}

但我可以在 "item" 中找到 aClientPath 的路径。使用上面的location节点,如何删除或修改权限?

这是配置管理器提供的修改权限的示例代码。它将根据多个属性搜索元素。

不确定是否达到您的要求。

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 authorizationSection = config.GetSection("system.ftpServer/security/authorization", "ftp/LDNClient/Reports/aClientPath");

            ConfigurationElementCollection authorizationCollection = authorizationSection.GetCollection();

            ConfigurationElement addElement = FindElement(authorizationCollection, "add", "users", @"LDNClient", "roles", @"", "permissions", @"3");
            if (addElement == null) throw new InvalidOperationException("Element not found!");

            addElement["permissions"] = @"Read";

            serverManager.CommitChanges();
        }
    }

    private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) {
        foreach (ConfigurationElement element in collection) {
            if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) {
                bool matches = true;

                for (int i = 0; i < keyValues.Length; i += 2) {
                    object o = element.GetAttributeValue(keyValues[i]);
                    string value = null;
                    if (o != null) {
                        value = o.ToString();
                    }

                    if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) {
                        matches = false;
                        break;
                    }
                }
                if (matches) {
                    return element;
                }
            }
        }
        return null;
    }
}

我不完全明白为什么必须这样,但我查看了 applicationHost.config 文件并复制了它。删除了 "write" 并添加了 "read, write"。有效,所以我会说它足够好。

                ConfigurationElement addElement = authorizationCollection.CreateElement("remove");
                addElement["users"] = @"LDNClient";
                addElement["roles"] = @"";
                addElement["permissions"] = @"Write";
                authorizationCollection.Add(addElement);

                addElement = authorizationCollection.CreateElement("add");
                addElement["accessType"] = @"Allow";
                addElement["users"] = @"LDNClient";
                addElement["roles"] = @"";
                addElement["permissions"] = @"Read, Write";
                authorizationCollection.Add(addElement);

                serverManager.CommitChanges();