Condividi tramite


Autorizzazione <personalizzata FTPAutorizzazione personalizzata>

Panoramica

L'elemento <customAuthorization> specifica le impostazioni per l'autorizzazione personalizzata di un sito FTP. Questa forma di autorizzazione usa provider di autorizzazioni personalizzati per convalidare l'accesso utente.

Se si abilita un provider di autorizzazione personalizzato, il provider di autorizzazione predefinito non verrà usato e non sarà possibile aggiungere manualmente una regola di autorizzazione o una regola di negazione alla configurazione.

Per informazioni su come creare un provider personalizzato, vedere How to Use Managed Code (C#) to Create a Simple FTP Home Directory Provider.For information about how to create a custom provider, see How to Use Managed Code (C#) to Create a Simple FTP Home Directory Provider.

Compatibilità

Versione Note
IIS 10.0 L'elemento <customAuthorization> non è stato modificato in IIS 10.0.
IIS 8,5 L'elemento <customAuthorization> non è stato modificato in IIS 8.5.
IIS 8,0 L'elemento <customAuthorization> è stato introdotto in IIS 8.0.
IIS 7,5 N/D
IIS 7.0 N/D
IIS 6.0 N/D

Installazione

Per supportare l'autorizzazione FTP usando un provider personalizzato nel sito FTP, è necessario installare il servizio FTP con estendibilità FTP.

Windows Server 2012

  1. Premere il tasto logo Windows e quindi fare clic su Server Manager.

  2. In Server Manager fare clic su Gestisci e quindi su Aggiungi ruoli e funzionalità.

  3. Nella procedura guidata Aggiungi ruoli e funzionalità :

    • Nella pagina Prima di iniziare fare clic su Avanti.
    • Nella pagina Tipo di installazione selezionare il tipo di installazione e quindi fare clic su Avanti.
    • Nella pagina Selezione server selezionare il server appropriato e quindi fare clic su Avanti.
    • Nella pagina Ruoli server verificare che sia selezionato Server Web (IIS) e quindi espanderlo.
    • Espandere Server FTP, quindi selezionare sia il servizio FTP che l'estendibilità FTP, quindi fare clic su Avanti.
    • Nella pagina Funzionalità fare clic su Avanti.
    • Nella pagina Conferma selezioni per l'installazione fare clic su Installa.
    • Nella pagina Risultati fare clic su Chiudi.

Windows 8

  1. Aprire il Pannello di controllo di Windows.
  2. In Windows Pannello di controllo aprire Programmi e funzionalità.
  3. In Programmi e funzionalità, fare clic su Attiva o disattiva le funzionalità di Windows.
  4. Nella finestra di dialogo Funzionalità di Windows espandere Internet Information Services, quindi espandere Server FTP.
  5. In Server FTP selezionare Ftp Service and FTP Extensibility (Servizio FTP) e FTP Extensibility (Estendibilità FTP) e quindi fare clic su OK.

Procedure

Come configurare l'autorizzazione FTP in base a un provider personalizzato

  1. Aprire Gestione Internet Information Services (IIS):

    • Se si usa Windows Server 2012 o versione successiva:

      • Sulla barra delle applicazioni fare clic su Server Manager, scegliere Strumenti, quindi fare clic su Gestione Internet Information Services (IIS).
    • Se si usa Windows 8 o versione successiva:

      • Tenere premuto il tasto Windows, premere la lettera X e quindi fare clic su Pannello di controllo.
      • Fare clic su Strumenti di amministrazione, quindi fare doppio clic su Gestione Internet Information Services (IIS).
  2. Nel riquadro Connessioni selezionare il nome del server, espandere Siti e quindi selezionare un sito FTP.

  3. Nel riquadro Home fare doppio clic sulla funzionalità Regole di autorizzazione FTP .

  4. Nel riquadro Azioni fare clic su Modifica impostazioni funzionalità.

  5. Nella finestra di dialogo Impostazioni funzionalità autorizzazione selezionare Scegliere un provider di autorizzazioni personalizzato per abilitare l'autorizzazione FTP da parte di un provider personalizzato. Nell'elenco a discesa associato selezionare un provider personalizzato dall'elenco.

    Screenshot della finestra di dialogo Impostazioni funzionalità autorizzazione.

    Nota

    Quando è stato abilitato un provider di autorizzazione FTP personalizzato, la funzionalità Regole di autorizzazione FTP è disabilitata.

  6. Fare clic su OK.

Configurazione

Attributi

Nessuno.

Elementi figlio

Elemento Descrizione
provider Elemento facoltativo.

Specifica il provider di autorizzazioni personalizzato.

Esempio di configurazione

Nell'esempio seguente viene visualizzato un <customAuthorization> elemento :

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

Nell'esempio seguente viene visualizzato un < elemento providerDefinitions> per il provider di autorizzazioni personalizzato nell'esempio precedente:

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

Codice di esempio

Gli esempi di codice seguenti configurano un provider di autorizzazioni personalizzato.

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

Nota

È necessario assicurarsi di impostare il parametro commit su apphost quando si usa AppCmd.exe per configurare queste impostazioni. In questo modo le impostazioni di configurazione vengono confermate nella sezione relativa al percorso appropriato nel file 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"