Windows 認証プロバイダー <providers>

  • 概要
  • 互換性
  • セットアップ
  • 方法
  • 構成
  • サンプル コード

※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。

概要

<windowsAuthentication> 要素の <providers> コレクションは、インターネット インフォメーション サービス (IIS) 7.0 の Windows 認証モジュールで使用する認証プロバイダーの一覧を定義します。このプロバイダーの一覧は拡張できず、既定では次の 2 つのエントリのみが含まれます。

  • Negotiate - このプロバイダーは、認証に Kerberos を使用します (使用可能な場合)。
  • NTLM - このプロバイダーは、認証に Windows NT LAN マネージャーを使用します。

互換性

  IIS 7.0 IIS 6.0
説明 <windowsAuthentication><providers> は IIS 7.0 で新たに導入された要素です。 <providers> コレクションは、IIS 6.0 の NTAuthenticationProviders メタベース プロパティに代わるものです。

セットアップ

IIS 7.0 の既定のインストールには、"Windows 認証" 役割サービスは含まれません。IIS で Windows 認証を使用するには、この役割サービスをインストールして、Web サイトまたはアプリケーションで匿名認証を無効にし、Windows 認証を有効にする必要があります。

: この役割サービスをインストールすると、IIS 7.0 は次の構成設定を ApplicationHost.config ファイルに適用します。

<windowsAuthentication enabled='false' />

Windows Server 2008

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

  2. [サーバー マネージャー] のツリー表示で、[役割] を展開して [Web サーバー (IIS)] をクリックします。

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

  4. 役割サービスの追加ウィザードの [役割サービスの選択] ページで、[Windows 認証] を選択して、[次へ] をクリックします。

    拡大

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

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

Windows Vista

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

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

  3. [Internet Information Services][World Wide Web サービス][セキュリティ] の順に展開します。

  4. [Windows 認証] を選択して、[OK] をクリックします。

    拡大

方法

IIS 7.0 には、Windows 認証プロバイダー用のユーザー インターフェイスはありません。Windows 認証プロバイダーの一覧をプログラムによって変更する方法の例については、このドキュメントの「サンプル コード」セクションを参照してください。

構成

属性

なし。

子要素

要素 説明
add オプションの要素。

セキュリティ プロバイダーをプロバイダーのコレクションに追加します。Windows 認証には少なくとも 1 つのプロバイダーが必要です。
remove オプションの要素。

プロバイダー コレクションからセキュリティ プロバイダーの参照を削除します。
clear オプションの要素。

プロバイダー コレクションからすべてのプロバイダーの参照を削除します。

構成サンプル

次の既定の <windowsAuthentication> 要素は、IIS 7.0 のルート ApplicationHost.config ファイルに構成されます。これにより、Windows 認証は既定で無効になっています。また、IIS 7.0 の 2 つの Windows 認証プロバイダーを定義しています。

<windowsAuthentication enabled="false">
   <providers>
      <add value="Negotiate" />
      <add value="NTLM" />
   </providers>
</windowsAuthentication>

次の例では、Contoso という名前の Web サイトで、Windows 認証を有効にし、匿名認証を無効にします。

<location path="Contoso">
   <system.webServer>
      <security>
         <authentication>
            <anonymousAuthentication enabled="false" />

            <windowsAuthentication enabled="true" />
         </authentication>
      </security>
   </system.webServer>
</location>

サンプル コード

次のコード サンプルでは、Contoso という名前のサイトで Windows 認証を有効にして、Negotiate プロバイダーを削除します。

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/windowsAuthentication /enabled:"True" /commit:apphost

appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/windowsAuthentication /-"providers.[value='Negotiate']" /commit:apphost

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 windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso");
         windowsAuthenticationSection["enabled"] = true;

         ConfigurationElementCollection providersCollection = windowsAuthenticationSection.GetCollection("providers");
         ConfigurationElement addElement = FindElement(providersCollection, "add", "value", @"Negotiate");

         if (addElement == null) throw new InvalidOperationException("Element not found!");
         providersCollection.Remove(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 windowsAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso")
      windowsAuthenticationSection("enabled") = True

      Dim providersCollection As ConfigurationElementCollection = windowsAuthenticationSection.GetCollection("providers")
      Dim addElement As ConfigurationElement = FindElement(providersCollection, "add", "value", "Negotiate")

      If (addElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If
      providersCollection.Remove(addElement)

      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 windowsAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/windowsAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso");
windowsAuthenticationSection.Properties.Item("enabled").Value = true;

var providersCollection = windowsAuthenticationSection.ChildElements.Item("providers").Collection;
var addElementPos = FindElement(providersCollection, "add", ["value", "Negotiate"]);
if (addElementPos == -1) throw "Element not found!";
providersCollection.DeleteElement(addElementPos);

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 = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set windowsAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/windowsAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso")
windowsAuthenticationSection.Properties.Item("enabled").Value = True

Set providersCollection = windowsAuthenticationSection.ChildElements.Item("providers").Collection
addElementPos = FindElement(providersCollection, "add", Array("value", "Negotiate"))

If (addElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If
providersCollection.DeleteElement(addElementPos)

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 value = Null Then
               value = CStr(value)
            End If
            If Not value = 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