基本驗證基本驗證 <>

概觀

元素 <basicAuthentication> 包含 Internet Information Services (IIS) 7 基本驗證模組的組態設定。 您可以設定這個專案來啟用或停用基本驗證、識別領域和預設登入網域,以及判斷模組所使用的登入方法。

基本驗證配置是廣泛使用的業界標準方法來收集使用者名稱和密碼資訊。 基本驗證會以未加密的形式,跨網路傳輸使用者名稱和密碼。 您可以使用 SSL 加密搭配基本驗證,協助保護透過網際網路或公司網路傳輸的使用者帳戶資訊。

相容性

版本 備註
IIS 10.0 <basicAuthentication> IIS 10.0 中未修改專案。
IIS 8.5 <basicAuthentication> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <basicAuthentication>
IIS 7.5 <basicAuthentication> 在 IIS 7.5 中修改專案。
IIS 7.0 專案 <basicAuthentication> 是在 IIS 7.0 中引進的。
IIS 6.0 元素 <basicAuthentication> 會取代 IIS 6.0 AuthTypeAuthFlags 中繼基底屬性的部分。

安裝程式

IIS 7 和更新版本的預設安裝不包含基本驗證角色服務。 若要在 Internet Information Services 上使用基本驗證 (IIS) ,您必須安裝角色服務、停用網站或應用程式的匿名驗證,然後啟用網站或應用程式的基本驗證。

若要安裝基本驗證角色服務,請使用下列步驟。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在工作列上,按一下 [伺服器管理員]
  2. 伺服器管理員中,按一下 [管理]功能表,然後按一下 [新增角色和功能]。
  3. 在 [ 新增角色和功能 精靈] 中,按 [下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [ 下一步]。
  4. 在 [ 伺服器角色] 頁面上,依序展開 [Web 服務器] ([IIS) ]、[ 網頁伺服器]、[ 安全性],然後選取 [ 基本驗證]。 按一下 [下一步] 。
    Windows Server 2012 介面中選取 [基本驗證] 的螢幕擷取畫面。 .
  5. 在 [選取功能] 頁面上,按 [下一步]
  6. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  7. 在 [結果] 頁面上,按一下 [關閉]

Windows 8 或Windows 8.1

  1. 在 [開始] 畫面上,將指標全部移至左下角,以滑鼠右鍵按一下 [開始] 按鈕,然後按一下[主控台]。
  2. 主控台中,按一下 [程式和功能],然後按一下 [開啟或關閉 Windows 功能]。
  3. 展開 [Internet Information Services]、[ 萬維網服務]、[ 安全性],然後選取 [ 基本驗證]。
    Windows 8 介面中選取 [基本驗證] 的螢幕擷取畫面。
  4. 按一下 [確定]。
  5. 按一下 [關閉] 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在工作列上,按一下 [開始],指向 [系統管理工具],然後按一下[伺服器管理員]。
  2. [伺服器管理員階層] 窗格中,展開 [角色],然後按一下 [Web 服務器] (IIS)
  3. [Web 服務器 (IIS) ] 窗格中,捲動至 [ 角色服務 ] 區段,然後按一下 [ 新增角色服務]。
  4. 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,選取 [基本驗證],然後按 [下一步]。 Windows Server 2008 介面中選取 [基本驗證] 的螢幕擷取畫面。
  5. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  6. 在 [結果] 頁面上,按一下 [關閉]

Windows Vista 或 Windows 7

  1. 在工作列上,按一下 [開始],然後按一下[主控台]。
  2. 主控台中,按一下 [程式和功能],然後按一下 [開啟或關閉 Windows 功能]。
  3. 序展開 [Internet Information Services]、[ 萬維網服務]、[ 安全性]、選取 [ 基本驗證],然後按一下 [ 確定]。
    Windows Vista 或 Windows 7 介面中選取 [基本驗證] 的螢幕擷取畫面。

作法

如何啟用基本驗證並停用匿名驗證

  1. (IIS) 管理員開啟 Internet Information Services

    • 如果您使用 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. 在 [ 連線 ] 窗格中,展開伺服器名稱、 [月臺],然後按一下您要啟用基本驗證的網站、應用程式或 Web 服務。

  3. 捲動至 [首頁] 窗格中的 [安全性] 區段,然後按兩下 [驗證]。

  4. 在 [ 驗證] 窗格中,選取 [ 基本驗證],然後在 [ 動作 ] 窗格中,按一下 [ 啟用]。

  5. 在 [驗證]窗格中,選取 [匿名驗證],然後按一下 [動作] 窗格中的 [停用]。
    在 [驗證] 窗格中選取 [匿名驗證] 的螢幕擷取畫面。

組態

專案 <basicAuthentication> 可在月臺、應用程式、虛擬目錄和 URL 層級設定。 安裝角色服務之後,IIS 7 會將下列組態設定認可至ApplicationHost.config檔案。

<basicAuthentication enabled='false' />

屬性

屬性 描述
defaultLogonDomain 選擇性字串屬性。

指定基本驗證的預設登入網域。
enabled 選擇性的 Boolean 屬性。

指定是否啟用基本驗證。

預設值是 false
logonMethod 選擇性列舉屬性。

logonMethod屬性可以是下列其中一個可能的值。 預設值為 ClearText
描述
Batch 此登入類型適用于批次伺服器,其中進程可能會代表使用者執行,而不需要該使用者的直接介入。

數值為 1
ClearText 此登入類型會保留驗證套件中的名稱和密碼,這可讓伺服器在模擬用戶端時連線到其他網路伺服器。

數值為 3
Interactive 此登入類型適用于以互動方式使用電腦的使用者。

數值為 0
Network 此登入類型適用于高效能伺服器來驗證純文字密碼。 此登入類型不會快取認證。

數值為 2
realm 選擇性字串屬性。

指定基本驗證的領域。

子元素

無。

組態範例

下列組態範例會啟用網站、Web 應用程式或 Web 服務的基本驗證。 根據預設,這些設定必須包含在您的ApplicationHost.config檔案中,而且您必須將它們包含在 元素中 <location> ,並使用 path 屬性來定義您要套用驗證設定的網站或應用程式。

<security>
   <authentication>
      <anonymousAuthentication enabled="false" />
      <basicAuthentication enabled="true" />
   </authentication>
</security>

範例程式碼

下列範例會啟用月臺的基本驗證。

AppCmd.exe

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

注意

當您使用AppCmd.exe設定這些設定時,請務必將 認可 參數 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 basicAuthenticationSection = config.GetSection("system.webServer/security/authentication/basicAuthentication", "Contoso");
            basicAuthenticationSection["enabled"] = true;

            serverManager.CommitChanges();
        }
    }
}

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 basicAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/basicAuthentication", "Contoso")
        basicAuthenticationSection("enabled") = True

        serverManager.CommitChanges()
    End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var basicAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/basicAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso");
basicAuthenticationSection.Properties.Item("enabled").Value = true;

adminManager.CommitChanges();

VBScript

Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set basicAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/basicAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso")
basicAuthenticationSection.Properties.Item("enabled").Value = True

adminManager.CommitChanges()