概述
<providers> 元素的 <add> 元素将功能提供程序添加到 FTP 站点的 FTP 自定义提供程序集合中。
注意
添加到此集合的提供程序可以实现自定义日志记录或主目录查找;实现身份验证和角色查找的自定义 FTP 提供程序将添加到 <ftpServer/security/authentication/customAuthentication> 集合中。
注意
添加到 <customFeatures/providers> 元素的自定义功能必须在 <system.ftpServer/providerDefinitions> 集合中注册。
注意
FTP 7.5 中引入了对创建自定义功能提供程序的支持。 有关如何创建 FTP 自定义功能提供程序的其他信息,请参阅 Microsoft IIS.net 网站上的针对 FTP 7.5 进行开发主题。
兼容性
| 版本 | 说明 | 
|---|---|
| IIS 10.0 | <add>元素在 IIS 10.0 中未进行修改。 | 
| IIS 8.5 | <add>元素在 IIS 8.5 中未进行修改。 | 
| IIS 8.0 | <add>元素在 IIS 8.0 中未进行修改。 | 
| IIS 7.5 | <providers>元素的<add>元素作为 IIS 7.5 的功能提供。 | 
| IIS 7.0 | <providers>元素的<add>元素是在 IIS 7.0 的独立下载项 FTP 7.0 中引入的。 | 
| IIS 6.0 | IIS 6.0 中的 FTP 服务不支持扩展性。 | 
如果使用 Windows 7 和 Windows Server 2008 R2,FTP 7.5 服务作为 IIS 7.5 的功能提供,因此无需下载 FTP 服务。
安装
若要支持 Web 服务器的 FTP 发布,必须安装 FTP 服务。 为此,请按照以下步骤操作。
Windows Server 2012 或 Windows Server 2012 R2
- 在任务栏上,单击 “服务器管理器”。 
- 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。 
- 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。 
- 在“服务器角色”页上,展开“Web 服务器(IIS)”,然后选择“FTP 服务器”。 
- 单击“下一步”,然后在“选择功能”页上,再次单击“下一步”。 
- 在“确认安装选择”页上,单击“安装”。 
- 在“结果” 页面中单击“关闭” 。 
Windows 8 或 Windows 8.1
- 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。 
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。 
- 展开“Internet Information Services”,然后选择“FTP 服务器”。 
- 单击“确定”。 
- 单击“关闭” 。 
Windows Server 2008 R2
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。 
- 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。 
- 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。 
- 在“添加角色服务向导”的“选择角色服务”页,展开“FTP 服务器”。 
- 选择“FTP 服务”。 
- 单击 “下一步” 。 
- 在“确认安装选择”页中,单击“安装”。 
- 在“结果” 页面中单击“关闭” 。 
Windows 7
- 在任务栏上,单击“开始”,然后单击“控制面板”。 
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。 
- 展开“Internet Information Services”,然后展开“FTP 服务器”。 
- 选择“FTP 服务”。 
- 单击“确定”。 
Windows Server 2008 或 Windows Vista
- 从以下 URL 下载安装包: 
- 按照以下演练中的说明安装 FTP 服务: 
操作方式
目前,没有用户界面可用于向站点添加自定义功能。 有关如何向 FTP 站点添加自定义功能的其他信息,请参阅本文档的配置和示例代码部分。
配置
<providers> 元素的 <add> 元素在站点级别配置。
特性
| 属性 | 说明 | 
|---|---|
| name | 必需的字符串属性。 在 <system.ftpServer/providerDefinitions>集合中注册的 FTP 提供程序的名称。没有默认值。 | 
| enabled | 可选布尔属性。 如果启用提供程序,则为 true;否则为 false。 默认值为 true。 | 
子元素
无。
配置示例
以下示例显示 FTP 站点的 <customFeatures> 元素,该元素将名为“FtpLoggingDemo”的自定义日志提供程序添加到 FTP 站点。
<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>
    <customFeatures>
      <providers>
        <add name="FtpLoggingDemo" enabled="true" />
      </providers>
    </customFeatures>
    <security>
      <authentication>
        <basicAuthentication enabled="true" />
        <anonymousAuthentication enabled="false" />
      </authentication>
    </security>
  </ftpServer>
</site>
代码示例
以下示例演示如何为 FTP 站点添加自定义日志提供程序。
注意
FTP 自定义功能必须在 <system.ftpServer/providerDefinitions> 集合中注册。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /+"[name='ftp.example.com'].ftpServer.customFeatures.providers.[name='CustomLoggingModule',enabled='True']" /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 customFeaturesElement = ftpServerElement.GetChildElement("customFeatures");
         ConfigurationElementCollection providersCollection = customFeaturesElement.GetCollection("providers");
         ConfigurationElement addElement = providersCollection.CreateElement("add");
         addElement["name"] = @"CustomLoggingModule";
         addElement["enabled"] = true;
         providersCollection.Add(addElement);
         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 customFeaturesElement = ftpServerElement.ChildElements.Item("customFeatures");
var providersCollection = customFeaturesElement.ChildElements.Item("providers").Collection;
var addElement = providersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "CustomLoggingModule";
addElement.Properties.Item("enabled").Value = true;
providersCollection.AddElement(addElement);
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 customFeaturesElement = ftpServerElement.ChildElements.Item("customFeatures")
Set providersCollection = customFeaturesElement.ChildElements.Item("providers").Collection
Set addElement = providersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "CustomLoggingModule"
addElement.Properties.Item("enabled").Value = True
providersCollection.AddElement(addElement)
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



