FTP 消息 <messages>

概述

<messages> 元素用于指定 FTP 服务向 FTP 客户端显示的消息。 当 FTP 客户端连接到 FTP 站点时,将显示为 bannerMessagegreetingMessagemaxClientsMessage 属性指定的消息;当 FTP 客户端从 FTP 站点断开连接时,将显示由 exitMessage 属性指定的消息。

suppressDefaultBanner 属性可以与 bannerMessage 属性结合使用来伪装 FTP 服务器类型。 如果取消了默认横幅,并且尚未在 bannerMessage 属性中指定横幅消息,则 FTP 服务器将在 FTP 客户端连接到服务器时显示空白横幅。

expandVariables 属性指定是否在 FTP 消息中显示一组特定的用户变量。 如果此属性设置为 true,FTP 服务将在 FTP 消息中显示用户变量;否则,所有消息文本在输入后将立即显示。 支持的用户变量包括:

User 变量 说明
%BytesReceived% 当前会话中从服务器发送到客户端的字节数。
%BytesSent% 当前会话中从客户端发送到服务器的字节数。
%SessionID% 当前会话的唯一标识符。
%SiteName% 托管当前会话的 FTP 站点的名称。
%UserName% 当前登录用户的帐户名。

allowLocalDetailedErrors 属性指定当 FTP 客户端正在服务器自身上连接 FTP 服务器时,是否显示详细错误消息。 这些错误消息包含服务器管理员可用于排查问题的详细信息。 如果此属性设置为 true,FTP 服务将仅向本地主机显示详细的错误消息;否则,不会显示详细的错误消息。

兼容性

版本 说明
IIS 10.0 <messages> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <messages> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <messages> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <ftpServer> 元素的 <messages> 元素作为 IIS 7.5 的功能提供。
IIS 7.0 FTP 7.0 中引入了 <ftpServer> 元素的 <messages> 元素作为 IIS 7.0 独立下载项。
IIS 6.0 空值

注意

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 扩展性”
    Screenshot of F T P Service and F T P Extensibility selected in a Windows Server 2012 interface. .

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

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

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

Windows 8 或 Windows 8.1

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

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

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

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    Screenshot of F T P Service and F T P Extensibility selected in a Windows 8 interface.

  4. 单击“确定”。

  5. 单击“关闭” 。

Windows Server 2008 R2

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

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

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

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

  5. 选择“FTP 服务”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    Screenshot of F T P Service and F T P Extensibility selected in a Windows Server 2008 interface.

  6. 单击 “下一步”

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

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

Windows 7

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

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

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

  4. 选择“FTP 服务”

    注意

    若要支持 FTP 服务使用 ASP.Membership 身份验证或 IIS 管理器身份验证,还需选择“FTP 扩展性”
    Screenshot of F T P Service and F T P Extensibility selected in a Windows 7 interface.

  5. 单击“确定”。

Windows Server 2008 或 Windows Vista

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

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

操作方式

如何为 FTP 站点配置自定义消息

  1. 打开 Internet Information Services (IIS) 管理器:

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

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

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

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

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

  3. 在站点的“主页”窗格中,双击“FTP 消息”功能
    Screenshot of the F T P Messages options selected in the web site Home pane.

  4. 配置以下选项:

    • 单击以选择“禁止显示默认横幅”选项。
    • 单击以选择“在消息中支持用户变量”选项。
    • 在“横幅”消息中,键入“这是我的 FTP 服务器”。
    • 在“欢迎”消息中,键入“你好,%UserName%!”。
    • 在“退出”消息中,键入“再见 %UserName%!”。
      Screenshot of the Message Behavior set to Show detailed messages for local requests in the F T P Messages pane.
  5. 在“操作”窗格中,单击“应用”

配置

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

特性

属性 说明
allowLocalDetailedErrors 可选的 Boolean 属性。

如果应在本地显示详细的错误消息,则为 true;否则为 false

注意:无法远程显示详细的错误消息。

默认值为 true
bannerMessage 可选的字符串属性。

指定客户端连接到 FTP 服务器时显示的消息。

没有默认值。
exitMessage 可选的字符串属性。

指定客户端关闭与 FTP 服务器的连接时显示的消息。

没有默认值。
expandVariables 可选的 Boolean 属性。

如果应展开用户变量,则为 true;否则为 false

默认值为 false
greetingMessage 可选的字符串属性。

指定在客户端登录到 FTP 服务器后显示的消息。

没有默认值。
maxClientsMessage 可选的字符串属性。

指定当客户端尝试登录且 FTP 服务器上已达到最大客户端数时显示的消息。

没有默认值。
suppressDefaultBanner 可选的 Boolean 属性。

如果 FTP 服务应禁止显示默认的“Microsoft FTP 服务”横幅,则为 true;否则为 false

默认值为 false

子元素

无。

配置示例

以下示例显示 FTP 站点的 <messages> 元素,该元素将 FTP 服务配置为将多条自定义 FTP 消息返回到客户端,禁止显示默认 FTP 横幅,并向登录到本地 FTP 服务器的 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>
    <messages exitMessage="Goodbye %UserName%!"
      greetingMessage="Hello %UserName%!"
      bannerMessage="This my FTP server."
      suppressDefaultBanner="true"
      allowLocalDetailedErrors="true"
      expandVariables="true" />
    <security>
      <authentication>
        <basicAuthentication enabled="true" />
        <anonymousAuthentication enabled="false" />
      </authentication>
    </security>
  </ftpServer>
</site>

代码示例

以下示例配置 FTP 站点,使其不显示默认横幅并返回自定义横幅消息。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.messages.bannerMessage:"This is my FTP Server!" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.messages.suppressDefaultBanner:"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 messagesElement = ftpServerElement.GetChildElement("messages");
         messagesElement["bannerMessage"] = @"This is my FTP Server!";
         messagesElement["suppressDefaultBanner"] = true;

         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 messagesElement As ConfigurationElement = ftpServerElement.GetChildElement("messages")
      messagesElement("bannerMessage") = "This is my FTP Server!"
      messagesElement("suppressDefaultBanner") = True

      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 messagesElement = ftpServerElement.ChildElements.Item("messages");
messagesElement.Properties.Item("bannerMessage").Value = "This is my FTP Server!";
messagesElement.Properties.Item("suppressDefaultBanner").Value = true;

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 messagesElement = ftpServerElement.ChildElements.Item("messages")
messagesElement.Properties.Item("bannerMessage").Value = "This is my FTP Server!"
messagesElement.Properties.Item("suppressDefaultBanner").Value = True

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