0

在 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
}

但我可以在“项目”中找到 aClientPath 的路径。使用上面的位置节点,我将如何删除它或修改它的权限?

4

2 回答 2

0

我不完全理解为什么必须这样,但我查看了 applicationHost.config 文件并复制了它。删除了“写”并添加了“读,写”。有效,所以我会说它足够好。

                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();
于 2019-12-08T11:38:22.847 回答
0

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

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

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;
    }
}
于 2019-12-05T09:19:24.050 回答