次の方法で共有


FTP 認証 <authentication>

概要

<authentication> 要素には、FTP サイトの認証設定を指定します。 認証設定はサイト レベルでのみ構成できますが、FTP の <authorization> 設定は URL ごとに構成できます。

FTP サイトに対して構成できる認証形式は 4 種類あります。

  • 匿名認証: この形式の認証を使うと、サーバーまたはドメイン上にユーザー アカウントがなくても FTP サイトにアクセスできます。パブリック FTP サイトに最もよく使われます。 通常、ユーザーは ftp または anonymous のユーザー名を使ってログインします。ほとんどのユーザーは自分のメール アドレスをパスワードとして使いますが、これは必須ではありません。

  • 基本認証: この形式の認証では、ユーザーがログインする前に、サーバーまたはドメイン上に有効なユーザー アカウントが必要です。

    注: ファイル転送プロトコル (FTP) の設計により、ユーザー名とパスワードは FTP 経由でプレーン テキストで送信されるため、ネットワーク検出に対して脆弱です。 そのため、SSL による基本認証を使うことをお勧めします。

  • クライアント証明書認証: この形式の認証では、クライアント証明書を使って FTP クライアントを認証します。

  • カスタム認証: この形式の認証では、カスタム認証プロバイダーを使ってユーザー名とパスワードを検証します。 FTP 7.0 と FTP 7.5 には、次の 2 つのカスタム認証プロバイダーが付属しています。

    • ASP.NET メンバーシップ認証: これは、ASP.NET メンバーシップ データベースを使用して、ユーザー名とパスワードを検証します。 詳細については、Microsoft の IIS.NET Web サイトの「.NET メンバーシップ認証を使用した FTP の構成」トピックを参照してください。
    • IIS マネージャー認証: これは、IIS マネージャーの構成を使ってユーザー名とパスワードを検証します。 詳細については、Microsoft の IIS.NET Web サイトの IIS 7.0 マネージャー認証を使用して FTP を構成する方法に関するトピックを参照してください。

    カスタム認証プロバイダーを使用する主な利点は、ユーザー アカウントをご使用のサーバーやドメインに作成する必要がないことです。 これで、ネットワークのセキュリティが向上します。

互換性

バージョン メモ
IIS 10.0 <authentication> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <authentication> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <authentication> 要素は IIS 8.0 では変更されませんでした。
IIS 7.5 <security> 要素の <authentication> 要素は、IIS 7.5 の機能として付属しています。
IIS 7.0 <security> 要素の <authentication> 要素が、IIS 7.0 用の別個のダウンロードとして FTP 7.0 で導入されました。
IIS 6.0 <ftpServer> 要素とその子要素は、LM/MSFTPSVC メタベース パスにある IIS 6.0 の FTP 設定を置き換えます。

Note

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 サーバー] を選びます。

    Note

    FTP サービスに対して ASP.NET メンバーシップ認証または IIS マネージャー認証をサポートするには、[FTP Service] に加えて[FTP 拡張] も選択する必要があります。
    [Web Server I I S] ウィンドウが展開され、F T P 拡張機能が選択されている[役割と機能の追加ウィザード] の [サーバーの役割] ページの画像。 .

  5. [次へ] をクリックし、[機能の選択] ページで再度 [次へ] をクリックします。

  6. [インストール オプションの確認] ページで、[インストール] をクリックします。

  7. [結果] ページで、 [閉じる]をクリックします。

Windows 8 または Windows 8.1

  1. [スタート] 画面で、ポインターを左下隅まで移動し、[スタート] ボタンを右クリックし、[コントロール パネル] をクリックします。

  2. [コントロール パネル][プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。

  3. [インターネット インフォメーション サービス] を展開し、[FTP サーバー] を展開します。

    Note

    FTP サービスに対して ASP.NET メンバーシップ認証または IIS マネージャー認証をサポートするには、[FTP 拡張] も選択する必要があります。
    [インターネット インフォメーション サービスと F T P サーバー] ウィンドウが展開され、F T P 拡張機能が選択されている画像。

  4. OK をクリックします。

  5. 閉じるをクリックします。

Windows Server 2008 R2

  1. タスク バーで [スタート] をクリックし、[管理ツール] をポイントして、[サーバー マネージャ] をクリックします。

  2. [サーバー マネージャ] 階層ウィンドウで [役割] を展開し、[Web サーバー (IIS)] をクリックします。

  3. [Web サーバー (IIS)] ウィンドウで、[役割サービス] セクションまでスクロールし、[役割サービスの追加] をクリックします。

  4. [役割サービスの追加] ウィザード[役割サービスの選択] ページで [FTP サーバー] を展開します。

  5. [FTP Service] を選択します。

    Note

    FTP サービスに対して ASP.NET メンバーシップ認証または IIS マネージャー認証をサポートするには、[FTP 拡張] も選択する必要があります。
    [役割サービスの追加ウィザード] の [役割サービスの選択] のスクリーンショット。[F T P サーバー] ウィンドウが展開され、F T P サービスが強調表示されています。

  6. 次へ をクリックします。

  7. [インストール オプションの確認] ページで、[インストール] をクリックします。

  8. [結果] ページで、 [閉じる]をクリックします。

Windows 7

  1. タスク バーで、[スタート][コントロール パネル] の順にクリックします。

  2. [コントロール パネル][プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。

  3. [インターネット インフォメーション サービス][FTP サーバー] の順に展開します。

  4. [FTP Service] を選択します。

    Note

    FTP サービスに対して ASP.NET メンバーシップ認証または IIS マネージャー認証をサポートするには、[FTP 拡張] も選択する必要があります。
    [インターネット インフォメーション サービスと F T P サーバー] ウィンドウが展開され、F T P 拡張機能が選択されているスクリーンショット。

  5. OK をクリックします。

Windows Server 2008 または Windows Vista

  1. 次の URL からインストール パッケージをダウンロードします。

  2. 次のチュートリアルの手順に従って、FTP サービスをインストールします。

操作方法

FTP サイトに対する匿名認証を有効または無効にする方法

  1. 次のようにインターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] をクリックし、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンをクリックし、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウでサーバー名を展開し、[サイト] ノードを展開してから、サイトの名前をクリックします。

  3. サイトの [ホーム] ウィンドウで、[FTP 認証] 機能をダブルクリックします。

  4. [FTP 認証] ページで、[匿名認証] を選びます。

  5. [操作] ウィンドウで、[有効にする] をクリックして匿名認証を有効にするか、[無効にする] をクリックして匿名認証を無効にします。
    匿名認証オプションが選択されている F T P 認証ページの画像。


FTP サイト ウィザードを使用して匿名読み取りアクセス権を指定して FTP サイトを作成する方法

  1. 次のようにインターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] をクリックし、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンをクリックし、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ペインで、ツリーにある [サイト] ノードをクリックします。

  3. ツリーの [サイト] ノードを右クリックし、[FTP サイトの追加] をクリックするか、[操作] ペインの [FTP サイトの追加] をクリックします。

  4. FTP サイトの追加ウィザードが表示されたら次の操作を行います。

    • [FTP サイト名] ボックスに「My New FTP Site」と入力します。

    • [物理パス] ボックスには、次のいずれかのオプションを使用してコンテンツ ディレクトリを指定できます。

      • 省略記号 (...) ボタンをクリックし、FTP サイトのコンテンツを含むフォルダーに移動します。
      • ボックスにコンテンツ フォルダーへのパスを入力します。 パスを入力する場合は、パスに環境変数を使用できます。 たとえば、コンテンツ ディレクトリに "%SystemDrive%\inetpub\ftproot" を使用できます。
    • これらの項目が完了したら、[次へ] をクリックします。
      [F T P サイト名] フィールドに入力された [My New F T P Site]\(新しい F T P サイトの追加\) が表示されている、F T P サイトの追加ウィザードのサイト情報の画像。

  5. FTP サイトの追加ウィザードの 2 番めのページで、次の操作を行います。

    • [IP アドレス] ドロップダウンから FTP サイトの IP アドレスを選択するか、既定の選択値である [すべて未割り当て] を受け入れます。

    • [ポート] ボックスに FTP サイトの TCP/IP ポートを入力します。 既定では、FTP サイトとクライアントはポート 21 を使用します。 (注: 暗黙的 FTPS を指定するには、ポート 990 を使用する必要があります)。

    • FTP 仮想ホスト名を使用するには、[仮想ホスト名を有効にする] ボックスをオンにし、[仮想ホスト] ボックスに仮想ホスト名を入力します。

    • SSL オプションに、次のいずれかのオプションを選びます。

      • SSL オプションを無効にするには、SSL に [無し] を選びます。
      • FTP クライアントが FTP サーバーに接続するときに、必要に応じて FTP over SSL を使用できるようにするには、SSL に [許可] を選びます。
      • FTP クライアントが FTP サーバーに接続するときに、常に FTP over SSL を使用できるようにするには、SSL に [必要] を選びます。
      • SSL に [許可] または [必要] を選択した場合は、[SSL 証明書] ドロップダウン メニューから証明書を選択します。
    • これらの項目が完了したら、[次へ] をクリックします。
      ドロップダウン リストから選択した I P アドレスを示す、F T P サイトの追加ウィザードのバインドと S S L 設定の画像。

  6. 次のウィザード ページで次の操作を行います。

    • [認証] 設定に [匿名] を選びます。
    • [承認] 設定で、[アクセスの許可] ドロップダウンから [匿名ユーザー] を選びます。
    • [アクセス許可] オプションの [読み取り] をオンにします。
    • これらの項目が完了したら、[終了] をクリックします。
      [匿名] オプションが選択された [F T P の追加] ウィザードの認証と承認の設定の画像。

FTP サイトに対する基本認証を有効または無効にする方法

  1. 次のようにインターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] をクリックし、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンをクリックし、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウでサーバー名を展開し、[サイト] ノードを展開してから、サイトの名前をクリックします。

  3. サイトの [ホーム] ウィンドウで、[FTP 認証] 機能をダブルクリックします。

  4. [FTP 認証] ページで、[基本認証] を選びます。
    [基本認証] が選択されている F T P 認証の画像。

  5. [操作] ウィンドウで、[有効にする] をクリックして基本認証を有効にするか、[無効にする] をクリックして基本認証を無効にします。


FTP サイト ウィザードを使用して、基本認証と読み取り/書き込みアクセス権を指定して FTP サイトを作成する方法

  1. 次のようにインターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] をクリックし、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンをクリックし、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ペインで、ツリーにある [サイト] ノードをクリックします。

  3. ツリーの [サイト] ノードを右クリックし、[FTP サイトの追加] をクリックするか、[操作] ペインの [FTP サイトの追加] をクリックします。

  4. FTP サイトの追加ウィザードが表示されたら次の操作を行います。

    • [FTP サイト名] ボックスに「My New FTP Site」と入力します。

    • [物理パス] ボックスには、次のいずれかのオプションを使用してコンテンツ ディレクトリを指定できます。

      • 省略記号 (...) ボタンをクリックし、FTP サイトのコンテンツを含むフォルダーに移動します。
      • ボックスにコンテンツ フォルダーへのパスを入力します。 パスを入力する場合は、パスに環境変数を使用できます。 たとえば、コンテンツ ディレクトリに "%SystemDrive%\inetpub\ftproot" を使用できます。
    • これらの項目が完了したら、[次へ] をクリックします。
      [F T P サイト名] ボックスに入力された [My New F T P Site]\(新しい F T P サイトの追加\) ウィザードの [サイト情報] のスクリーンショット。

  5. FTP サイトの追加ウィザードの 2 番めのページで、次の操作を行います。

    • [IP アドレス] ドロップダウンから FTP サイトの IP アドレスを選択するか、既定の選択値である [すべて未割り当て] を受け入れます。

    • [ポート] ボックスに FTP サイトの TCP/IP ポートを入力します。 既定では、FTP サイトとクライアントはポート 21 を使用します。 (注: 暗黙的 FTPS を指定するには、ポート 990 を使用する必要があります)。

    • FTP 仮想ホスト名を使用するには、[仮想ホスト名を有効にする] ボックスをオンにし、[仮想ホスト] ボックスに仮想ホスト名を入力します。

    • SSL オプションに、次のいずれかのオプションを選びます。

      • SSL オプションを無効にするには、SSL に [無し] を選びます。
      • FTP クライアントが FTP サーバーに接続するときに、必要に応じて FTP over SSL を使用できるようにするには、SSL に [許可] を選びます。
      • FTP クライアントが FTP サーバーに接続するときに、常に FTP over SSL を使用できるようにするには、SSL に [必要] を選びます。
      • SSL に [許可] または [必要] を選択した場合は、[SSL 証明書] ドロップダウン メニューから証明書を選択します。
    • これらの項目が完了したら、[次へ] をクリックします。
      ドロップダウン メニューから選択した [I P アドレス] を示す F T P サイトの追加ウィザードの [Binding and S S L Settings]\(バインドと S S L 設定\) ページのスクリーンショット。

  6. 次のウィザード ページで次の操作を行います。

    • [認証] 設定に [基本] を選びます。
    • [承認] 設定で、[アクセスの許可] ドロップダウンから [指定されたユーザー] を選び、ドロップダウン メニューの下のボックスにアカウント名を入力します。
    • [アクセス許可] オプションの [読み取り][書き込み] をオンにします。
    • これらの項目が完了したら、[終了] をクリックします。
      [認証] で [基本] オプションが選択されていることを示す[F T P サイトの追加ウィザード] の [認証と承認の設定] のスクリーンショット。

構成

<authentication> 要素は、サイト レベルで構成されます。

属性

なし。

子要素

要素 説明
anonymousAuthentication 省略可能な要素です。

FTP サイトの匿名認証設定を指定します。
basicAuthentication 省略可能な要素です。

FTP サイトの基本認証設定を指定します。
clientCertAuthentication 省略可能な要素です。

FTP サイトのクライアント証明書認証設定を指定します。
customAuthentication 省略可能な要素です。

FTP サイトのカスタム認証設定を指定します。

注: カスタム認証は、カスタム認証プロバイダーを介して実装されます。

構成サンプル

次のサンプルは、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

Note

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

Note

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