FTP 安全<元素>

概述

<security> 元素为 FTP 站点的安全选项定义父元素,并包含以下子元素(分层显示):

  • <authentication> - 此子元素是 FTP 站点的各种身份验证设置的父元素,包含以下子元素:

    • <anonymousAuthentication> - 指定匿名访问的设置。 这种形式的身份验证允许访问 FTP 站点且无需在服务器或域中有用户帐户,最常用于公共 FTP 站点
    • <basicAuthentication> - 指定基本身份验证的设置。 此形式的身份验证需要用户使用服务器或域中的有效用户帐户,然后才能登录。
    • <clientCertAuthentication> - 指定是否启用 Active Directory 客户端证书身份验证。 这种形式的身份验证使用客户端证书对 FTP 客户端进行身份验证。
    • <customAuthentication> - 指定自定义身份验证的设置。 这种形式的身份验证使用自定义身份验证提供程序来验证用户名和密码。
  • <commandFiltering> - 指定 FTP 命令筛选的设置。 此集合允许单独允许或拒绝 FTP 服务的特定 FTP 命令。

  • <customAuthorization> - 指定自定义授权的设置。 此形式的授权使用内置授权提供程序或自定义授权提供程序来验证用户访问。

  • <dataChannelSecurity> - 指定 FTP 数据通道安全性的设置。 此安全功能强制要求会话的数据和控制通道必须具有匹配的客户端地址。

  • <ssl> - 指定 FTP 站点的安全套接字层 (SSL) 设置。

  • <sslClientCertificates> - 指定 FTP 站点的安全套接字层 (SSL) 客户端证书设置。

兼容性

版本 说明
IIS 10.0 <security> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <security> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 已将 <customAuthorization> 元素添加到 <security> 元素(作为 IIS 8.0 的功能)。
IIS 7.5 <ftpServer> 元素的 <security> 元素作为 IIS 7.5 的功能提供。
IIS 7.0 <ftpServer> 元素的 <security> 元素是在 IIS 7.0 的独立下载项 FTP 7.0 中引入的。
IIS 6.0 <ftpServer> 元素及其子元素取代了位于 LM/MSFTPSVC 元数据库路径中的 IIS 6.0 FTP 设置

注意

FTP 7.0 和 FTP 7.5 服务是 IIS 7.0 带外提供的,需要从以下 URL 下载和安装模块:

https://www.iis.net/expand/FTP

在 Windows 7 和 Windows Server 2008 R2 中,FTP 7.5 服务作为 IIS 7.5 的功能提供,因此无需再下载 FTP 服务。

安装

若要支持 Web 服务器的 FTP 发布,必须安装 FTP 服务。 为此,请按照以下步骤操作。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。

  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。

  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”

  4. 在“服务器角色”页上,展开“Web 服务器(IIS)”,然后选择“FTP 服务器”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,除“FTP 服务”外还需选择“FTP 扩展性”
    Web Server I S 和 F T P Server 的图像,其中突出显示了 F T P 扩展性。 .

  5. 单击“下一步”,然后在“选择功能”页上,再次单击“下一步”

  6. “确认安装选择”页上,单击“安装”

  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”

  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”

  3. 展开“Internet Information Services”,然后选择“FTP 服务器”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    已展开的 Internet Information Services 和 F T P 服务器窗格的屏幕截图,并选中了 F T P 扩展性。

  4. 单击“确定”。

  5. 单击“关闭” 。

Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”

  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。

  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”

  4. 在“添加角色服务向导”的“选择角色服务”页,展开“FTP 服务器”

  5. 选择“FTP 服务”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    从 F T P 服务器展开窗格中选择的 F T P 服务和 F T P 扩展性的图像。

  6. 单击 “下一步”

  7. “确认安装选择”页中,单击“安装”

  8. 在“结果” 页面中单击“关闭”

Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”

  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”

  3. 展开“Internet Information Services”,然后展开“FTP 服务器”

  4. 选择“FTP 服务”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    从 F T P 服务器和 Internet Information Services 展开窗格中选择的 F T P 扩展性的屏幕截图。

  5. 单击“确定”。

Windows Server 2008 或 Windows Vista

  1. 从以下 URL 下载安装包:

  2. 按照以下演练中的说明安装 FTP 服务:

操作方式

如何对 FTP 站点启用或禁用匿名身份验证

  1. 打开 Internet Information Services (IIS) Manager:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
  2. 在“连接”窗格中,展开服务器名称,展开“站点”节点,然后单击站点名称

  3. 在站点的“主页”窗格中,双击“FTP 身份验证”功能

  4. 在“FTP 身份验证”页,选择“匿名身份验证”

  5. 在“操作”窗格中,单击“启用”以启用匿名身份验证,或单击“禁用”以禁用匿名身份验证
    “操作”窗格中 F T P 身份验证的图像,其中显示已禁用匿名身份验证。


如何使用 FTP 站点向导创建具有匿名读取访问权限的 FTP 站点

  1. 打开 Internet Information Services (IIS) Manager:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
  2. 在“连接”窗格中,单击树中的“站点”节点

  3. 右键单击树中的“站点”节点,单击“添加 FTP 站点”,或者单击“操作”窗格中的“添加 FTP 站点”

  4. 显示“添加 FTP 站点”向导时

    • 在“FTP 站点名称”框中输入“我的新 FTP 站点”

    • 对于“物理路径”框,可以使用以下选项之一指定内容目录:

      • 单击省略号 (...) 按钮,然后导航到包含 FTP 站点内容的文件夹
      • 在框中键入内容文件夹的路径。 请注意,如果选择键入路径,则可以在路径中使用环境变量。 例如,可对内容路径使用“%SystemDrive%\inetpub\ftproot”。
    • 完成这些项,然后单击“下一步”
      “添加 F T P 网站”对话框的图像,其中“我的新 F T P 站点”键入了 F T 站点名称框。

  5. 在“添加 FTP 站点”向导的第二页:

    • 从“IP 地址”下拉菜单中选择 FTP 站点的 IP 地址,或者选择接受默认选择“所有未分配”

    • 在“端口”框中输入 FTP 站点的 TCP/IP 端口。 默认情况下,FTP 站点和客户端使用端口 21。 (注意:若要指定隐式 FTPS,需要使用端口 990。)

    • 若要使用 FTP 虚拟主机名,请选择“启用虚拟主机名”框,然后在“虚拟主机”框中输入虚拟主机名

    • 对于 SSL 选项,请选择以下选项之一:

      • 选择“无 SSL”以禁用 SSL 选项
      • 选择“允许 SSL”以允许 FTP 客户端在连接到 FTP 服务器时选择性地使用 FTP over SSL
      • 选择“要求 SSL”以允许 FTP 客户端在连接到 FTP 服务器时始终使用 FTP over SSL
      • 如果选择“允许 SSL”或“要求 SSL”,请从“SSL 证书”下拉菜单中选择证书
    • 完成这些项,然后单击“下一步”
      绑定和 S S L 设置 对话框的图像,其中“绑定”部分中的“I P 地址”框。

  6. 在向导的下一页:

    • 为“身份验证”设置选择“匿名”
    • 对于“授权”设置,请从“允许以下对象访问”下拉列表中选择“匿名用户”
    • 为“权限”选项选择“读取”
    • 完成这些项,然后单击“完成”
      “身份验证和授权”设置的图像,其中选择了“匿名”选项。

如何对 FTP 站点启用或禁用基本身份验证

  1. 打开 Internet Information Services (IIS) Manager:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
  2. 在“连接”窗格中,展开服务器名称,展开“站点”节点,然后单击站点名称

  3. 在站点的“主页”窗格中,双击“FTP 身份验证”功能

  4. 在“FTP 身份验证”页上,选择“基本身份验证”。
    F T P 身份验证的屏幕截图,其中显示了匿名身份验证和基本身份验证选项。

  5. 在“操作”窗格中,单击“启用”以启用基本身份验证,或单击“禁用”以禁用基本身份验证


如何使用 FTP 站点向导创建具有基本身份验证和读/写访问权限的 FTP 站点

  1. 打开 Internet Information Services (IIS) Manager:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
  2. 在“连接”窗格中,单击树中的“站点”节点

  3. 右键单击树中的“站点”节点,单击“添加 FTP 站点”,或者单击“操作”窗格中的“添加 FTP 站点”

  4. 显示“添加 FTP 站点”向导时

    • 在“FTP 站点名称”框中输入“我的新 FTP 站点”

    • 对于“物理路径”框,可以使用以下选项之一指定内容目录:

      • 单击省略号 (...) 按钮,然后导航到包含 FTP 站点内容的文件夹
      • 在框中键入内容文件夹的路径。 请注意,如果选择键入路径,则可以在路径中使用环境变量。 例如,可对内容路径使用“%SystemDrive%\inetpub\ftproot”。
    • 完成这些项,然后单击“下一步”
      “添加 F T P 网站向导”上的网站信息的图像,其中显示了“我的新 F T P 站点”,在 F T P 网站名称字段中键入。

  5. 在“添加 FTP 站点”向导的第二页:

    • 从“IP 地址”下拉菜单中选择 FTP 站点的 IP 地址,或者选择接受默认选择“所有未分配”

    • 在“端口”框中输入 FTP 站点的 TCP/IP 端口。 默认情况下,FTP 站点和客户端使用端口 21。 (注意:若要指定隐式 FTPS,需要使用端口 990。)

    • 若要使用 FTP 虚拟主机名,请选择“启用虚拟主机名”框,然后在“虚拟主机”框中输入虚拟主机名

    • 对于 SSL 选项,请选择以下选项之一:

      • 选择“无 SSL”以禁用 SSL 选项
      • 选择“允许 SSL”以允许 FTP 客户端在连接到 FTP 服务器时选择性地使用 FTP over SSL
      • 选择“要求 SSL”以允许 FTP 客户端在连接到 FTP 服务器时始终使用 FTP over SSL
      • 如果选择“允许 SSL”或“要求 SSL”,请从“SSL 证书”下拉菜单中选择证书
    • 完成这些项,然后单击“下一步”
      “添加 F T P 站点向导”中绑定和 S S L 设置的图像,其中显示了从下拉列表中选择的 I P 地址。

  6. 在向导的下一页:

    • 对于“身份验证”设置,选择“基本”
    • 对于“授权”设置,请从“允许访问”下拉列表中选择“指定用户”,然后在下拉菜单下方的框中输入帐户名称。
    • 对于“权限”选项,选择“读取”和“写入”
    • 完成这些项,然后单击“完成”
      “添加 F T P 站点向导”中“身份验证和授权”设置的屏幕截图,其中选择了“基本”选项。

配置

<security> 元素在站点级别配置。

特性

无。

子元素

元素 说明
authentication 可选元素。

指定 FTP 站点的身份验证设置。
commandFiltering 可选元素。

指定 FTP 命令筛选的设置。
customAuthorization 可选元素。

指定用于 FTP 授权的自定义提供程序。
dataChannelSecurity 可选元素。

指定 FTP 数据通道安全性的设置。
ssl 可选元素。

指定 FTP 站点的 SSL 设置。
sslClientCertificates 可选元素。

指定 FTP 站点的 SSL 客户端证书选项。

配置示例

以下示例演示 FTP 站点的 <ftpServer> 元素中的多个配置设置。 更具体而言,此示例中的 <site> 设置演示如何:

  • 创建 FTP 站点并在端口 21 上为 FTP 协议添加绑定。
  • 配置 FTP SSL 选项,以允许使用证书对控件和数据信道进行安全访问。
  • 禁用“匿名身份验证”,然后为 FTP 启用“基本身份验证”。
  • 拒绝 FTP SYST 命令的访问
  • 指定 UNIX 目录列表格式。
  • 配置日志记录选项。
  • 指定自定义欢迎消息并启用本地详细错误消息。
  • 指定用户从基于其登录名的主目录中开始,前提是该目录存在。
<site name="ftp.example.com" id="5">
  <application path="/">
    <virtualDirectory path="/" physicalPath="c:\inetpub\www.example.com" />
  </application>
  <bindings>
    <binding protocol="ftp" bindingInformation="*:21:" />
  </bindings>
  <ftpServer>
    <security>
      <ssl controlChannelPolicy="SslAllow"
         dataChannelPolicy="SslAllow"
         serverCertHash="57686f6120447564652c2049495320526f636b73" />
      <authentication>
        <basicAuthentication enabled="true" />
        <anonymousAuthentication enabled="false" />
      </authentication>
      <commandFiltering maxCommandLine="4096" allowUnlisted="true">
        <add command="SYST" allowed="false" />
      </commandFiltering>
    </security>
    <directoryBrowse showFlags="StyleUnix" />
    <logFile logExtFileFlags="Date, Time, ClientIP, UserName, ServerIP, Method, UriStem, FtpStatus, Win32Status, ServerPort, FtpSubStatus, Session, FullPath, Info" />
    <messages expandVariables="true"
       greetingMessage="Welcome %UserName%!"
       allowLocalDetailedErrors="true" />
    <userIsolation mode="StartInUsersDirectory" />
  </ftpServer>
</site>

代码示例

以下示例禁用 FTP 站点的匿名身份验证。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.security.authentication.anonymousAuthentication.enabled:"False" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

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 sitesSection = config.GetSection("system.applicationHost/sites");
         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"ftp.example.com");
         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
         ConfigurationElement securityElement = ftpServerElement.GetChildElement("security");
         ConfigurationElement authenticationElement = securityElement.GetChildElement("authentication");
         ConfigurationElement anonymousAuthenticationElement = authenticationElement.GetChildElement("anonymousAuthentication");
         anonymousAuthenticationElement["enabled"] = false;

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

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection

      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "ftp.example.com")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim ftpServerElement As ConfigurationElement = siteElement.GetChildElement("ftpServer")
      Dim securityElement As ConfigurationElement = ftpServerElement.GetChildElement("security")
      Dim authenticationElement As ConfigurationElement = securityElement.GetChildElement("authentication")
      Dim anonymousAuthenticationElement As ConfigurationElement = authenticationElement.GetChildElement("anonymousAuthentication")
      anonymousAuthenticationElement("enabled") = False

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;

var siteElementPos = FindElement(sitesCollection, "site", ["name", "ftp.example.com"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var ftpServerElement = siteElement.ChildElements.Item("ftpServer");
var securityElement = ftpServerElement.ChildElements.Item("security");
var authenticationElement = securityElement.ChildElements.Item("authentication");
var anonymousAuthenticationElement = authenticationElement.ChildElements.Item("anonymousAuthentication");
anonymousAuthenticationElement.Properties.Item("enabled").Value = false;

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection

siteElementPos = FindElement(sitesCollection, "site", Array("name", "ftp.example.com"))
If siteElementPos = -1 Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)

Set ftpServerElement = siteElement.ChildElements.Item("ftpServer")
Set securityElement = ftpServerElement.ChildElements.Item("security")
Set authenticationElement = securityElement.ChildElements.Item("authentication")
Set anonymousAuthenticationElement = authenticationElement.ChildElements.Item("anonymousAuthentication")
anonymousAuthenticationElement.Properties.Item("enabled").Value = False

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function

以下示例禁用 FTP 站点的基本身份验证。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.security.authentication.basicAuthentication.enabled:"False" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

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 sitesSection = config.GetSection("system.applicationHost/sites");
         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"ftp.example.com");
         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
         ConfigurationElement securityElement = ftpServerElement.GetChildElement("security");
         ConfigurationElement authenticationElement = securityElement.GetChildElement("authentication");
         ConfigurationElement basicAuthenticationElement = authenticationElement.GetChildElement("basicAuthentication");
         basicAuthenticationElement["enabled"] = false;

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

}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection

      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "ftp.example.com")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim ftpServerElement As ConfigurationElement = siteElement.GetChildElement("ftpServer")
      Dim securityElement As ConfigurationElement = ftpServerElement.GetChildElement("security")
      Dim authenticationElement As ConfigurationElement = securityElement.GetChildElement("authentication")
      Dim basicAuthenticationElement As ConfigurationElement = authenticationElement.GetChildElement("basicAuthentication")
      basicAuthenticationElement("enabled") = False

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;

var siteElementPos = FindElement(sitesCollection, "site", ["name", "ftp.example.com"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var ftpServerElement = siteElement.ChildElements.Item("ftpServer");
var securityElement = ftpServerElement.ChildElements.Item("security");
var authenticationElement = securityElement.ChildElements.Item("authentication");
var basicAuthenticationElement = authenticationElement.ChildElements.Item("basicAuthentication");
basicAuthenticationElement.Properties.Item("enabled").Value = false;

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection

siteElementPos = FindElement(sitesCollection, "site", Array("name", "ftp.example.com"))
If siteElementPos = -1 Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)

Set ftpServerElement = siteElement.ChildElements.Item("ftpServer")
Set securityElement = ftpServerElement.ChildElements.Item("security")
Set authenticationElement = securityElement.ChildElements.Item("authentication")
Set basicAuthenticationElement = authenticationElement.ChildElements.Item("basicAuthentication")
basicAuthenticationElement.Properties.Item("enabled").Value = False

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function