FTP 自定义授权 <customAuthorization>

概述

<customAuthorization> 元素指定 FTP 站点的自定义授权设置。 此形式的授权使用自定义授权提供程序来验证用户访问。

如果启用自定义授权提供程序,则不会使用内置授权提供程序,并且你将无法手动向配置添加允许规则或拒绝规则。

有关如何创建自定义提供程序的信息,请参阅如何使用托管代码 (C#) 创建简单的 FTP 主目录提供程序

兼容性

版本 说明
IIS 10.0 <customAuthorization> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <customAuthorization> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <customAuthorization> 元素是在 IIS 8.0 中引入的。
IIS 7.5 空值
IIS 7.0 空值
IIS 6.0 空值

安装

若要在 FTP 站点上使用自定义提供程序支持 FTP 授权,必须安装具有 FTP 扩展性的 FTP 服务

Windows Server 2012

  1. 按 Windows 徽标键,然后单击“服务器管理器”

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

  3. 在“添加角色和功能”向导中

    • 开始之前页面上,单击下一步
    • 在“安装类型”页上,选择安装类型,然后单击“下一步”
    • 在“服务器选择”页上,选择适当的服务器,然后单击“下一步”
    • 在“服务器角色”页上,确保选中“Web 服务器(IIS)”,然后将其展开
    • 展开“FTP 服务器”,然后选择“FTP 服务”和“FTP 扩展性”,然后单击“下一步”
    • 在“功能”页上,单击“下一步”。
    • “确认安装选择”页中,单击“安装”
    • 在“结果” 页面中单击“关闭”

Windows 8

  1. 打开 Windows“控制面板”。
  2. 在 Windows 控制面板中,打开“程序和功能”
  3. 在“程序和功能”中,单击“打开或关闭 Windows 功能”
  4. 在“Windows 功能”对话框中,展开“Internet Information Services”,然后展开“FTP 服务器”
  5. 在“FTP 服务器”下面,选择“FTP 服务”和“FTP 扩展性”,然后单击“确定”

操作方式

如何基于自定义提供程序配置 FTP 授权

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

    • 如果使用 Windows Server 2012 或更高版本:

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

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet Information Services (IIS)管理器”。
  2. 在“连接”窗格中,选择服务器名称,展开“站点”,然后选择一个 FTP 站点

  3. 在“主页”窗格中,双击“FTP 授权规则”功能

  4. 在“操作”窗格中,单击“编辑功能设置”

  5. 在“授权功能设置”对话框中,选择“选择自定义授权提供程序”以启用自定义提供程序的 FTP 授权。 在关联的下拉列表中,从列表中选择自定义提供程序。

    Screenshot of the Authorization Feature Settings dialog box.

    注意

    启用自定义 FTP 授权提供程序后,将禁用“FTP 授权规则”功能

  6. 单击“确定”。

配置

特性

无。

子元素

元素 说明
provider 可选元素。

指定自定义授权提供程序。

配置示例

以下示例显示 <customAuthorization> 元素:

<ftpServer>
   <security>
      <customAuthorization>
         <provider name="MyProvider" enabled="true" />
      </customAuthorization>
   </security>
</ftpServer>

以下示例显示上一示例中自定义授权提供程序的 <providerDefinitions> 元素:

<system.ftpServer>
   <providerDefinitions>
      <add name="MyProvider" type="MyProvider, MyProvider, version=1.0.0.0, Culture=neutral, PublicKeyToken=426f62526f636b73" />
   </providerDefinitions>
</system.ftpServer>

代码示例

以下代码示例配置自定义授权提供程序。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /[name='MyFTPSite'].ftpServer.security.customAuthorization.provider.name:"MyProvider" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /[name='MyFTPSite'].ftpServer.security.customAuthorization.provider.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", @"MyFTPSite");
            if (siteElement == null) throw new InvalidOperationException("Element not found!");
            
            
            ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
            
            ConfigurationElement securityElement = ftpServerElement.GetChildElement("security");
            
            ConfigurationElement customAuthorizationElement = securityElement.GetChildElement("customAuthorization");
            
            ConfigurationElement providerElement = customAuthorizationElement.GetChildElement("provider");
            providerElement["name"] = @"MyProvider";
            providerElement["enabled"] = 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", "MyFTPSite")
         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 customAuthorizationElement As ConfigurationElement = securityElement.GetChildElement("customAuthorization")
         Dim providerElement As ConfigurationElement = customAuthorizationElement.GetChildElement("provider")
         providerElement("name") = "MyProvider"
         providerElement("enabled") = true
         serverManager.CommitChanges
     End Sub
     
     Private Shared Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ParamArray ByVal 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 = 0
                 Do While (i < keyValues.Length)
                     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
                     i = (i + 2)
                 Loop
                 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", "MyFTPSite"]);
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 customAuthorizationElement = securityElement.ChildElements.Item("customAuthorization");
var providerElement = customAuthorizationElement.ChildElements.Item("provider");
providerElement.Properties.Item("name").Value = "MyProvider";
providerElement.Properties.Item("enabled").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", "MyFTP"))
if (siteElementPos = -1) THEN throw "Element not found!"
Set siteElement = sitesCollection.Item(siteElementPos)

Set ftpServerElement = siteElement.ChildElements.Item("ftpServer")
Set securityElement = ftpServerElement.ChildElements.Item("security")
Set customAuthorizationElement = securityElement.ChildElements.Item("customAuthorization")
Set providerElement = customAuthorizationElement.ChildElements.Item("provider")
providerElement.Properties.Item("name").Value = "MyProvider1"
providerElement.Properties.Item("enabled").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

PowerShell

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='MyFTPSite']/ftpServer/security/customAuthorization/provider" -name "name" -value "MyProvider"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='MyFTPSite']/ftpServer/security/customAuthorization/provider" -name "enabled" -value "True"