如何在 IIS 7.0 組態中使用鎖定

Saad Ladki

摘要

本檔說明如何在伺服器上鎖定和解除鎖定組態。 您將瞭解應用層級組態檔可以覆寫的設定,以及如何使用 <location> 元素來鎖定整個區段。

您將在區段內,實驗更細微的組態設定鎖定,例如:

  • 鎖定特定元素和屬性
  • 鎖定特定專案或屬性 以外的 所有專案
  • 鎖定特定的集合指示詞,例如 <add><remove><clear> 指示詞
  • 鎖定集合中的特定元素

閱讀本檔之後,您將瞭解如何透過直接編輯組態檔中的 XML 元素來管理組態鎖定的不同功能, (程式設計介面來執行這些工作,以非常密切地遵循 XML 結構) 。

本檔只會刻意在編輯組態檔中的 XML 元素,而不是顯示使用系統管理 API、腳本或使用者介面 (UI) 來完成相同工作的方式。

簡介

IIS 7.0 和更新版本允許鎖定和解除鎖定各種層級和範圍的組態設定。 鎖定組態表示無法在階層的較低層級 (覆寫或設定所有) 。 解除鎖定組態只能在鎖定的層級完成。 例如,針對不同的月臺或路徑建立不同的組態時,這很有用,而且只允許某些網站和路徑覆寫它。 鎖定可以在區段層級或區段內的特定專案、屬性、集合元素和集合指示詞完成。

工作 1:使用 <location> 標記鎖定區段

在此工作中,您將瞭解如何使用 <location> 標籤來鎖定 (或解除) 鎖定全域層級的整個組態區段,以便無法在組態階層的應用程式層級覆寫它們。

注意

根據預設,applicationHost.config中的大部分 IIS 區段都會鎖定,而且不會鎖定 .NET Framework (包括 machine.config 和根web.config) 中 system.web > 區段群組中的 < ASP.NET 區段。

使用 [記事本] 之類的文字編輯器,在下列位置開啟applicationHost.config檔案:

%windir%\system32\inetsrv\config\applicationHost.config

<configSections>檢閱檔案頂端的 區段:其具有此檔案中組態區段的中繼資料,例如區段的名稱、包含區段群組,以及是否鎖定它們。

鎖定區段是由 「overrideModeDefault」 屬性所指定,也就是 「Allow」 或 「Deny」。 根據預設,很少區段不會鎖定,如下列這一行所指定:

<section name="defaultDocument" overrideModeDefault="Allow" />

在這裡,我們會處理 區 <windowsAuthentication> 段。 預設會鎖定它。

若要解除鎖定伺服器上所有應用程式的整個區段,請將內容從檔案的目前位置移至檔案底部,並將其放在 元素內 <location overrideMode="Allow"> 。 也請記得讓區段群組圍繞在周圍: <system.webServer ,然後 ><security<authentication> 。 > 最終結果看起來應該像這樣:

<location overrideMode="Allow">
  <system.webServer>
     <security>
        <authentication>
          <!-- the content of windowsAuthentication section is here -->
        </authentication>
     </security>
  </system.webServer>
</location>

區段現在已針對所有應用程式解除鎖定。 您可以在位置標籤上指定路徑,讓區段只會針對此路徑解除鎖定。 如上一個步驟) 未指定 (的預設路徑是 path=「.」 (或 path=「」) ,這表示「這個目前層級」。 在此情況下,由於這是applicationHost.config,所以目前的層級表示全域層級。 您也可以在命名空間階層中的任何位置使用位置標籤,例如在 vdir 層級的web.config,以從這點向下鎖定組態。

以下範例說明如何只針對 「AdminSuperTrusted」 網站解除鎖定此區段。 這表示web.config該網站的檔案可以覆寫本節中的設定;但是,針對方塊上的所有其他網站,它會在全域層級鎖定,而且無法覆寫。

在此範例中,您必須將區段的內容保留在其原始位置applicationHost.config,然後使用特定路徑在位置標記中指定區段:

<location path="AdminSuperTrustedSite" overrideMode="Allow">
  <system.webServer>
    <security>
      <authentication>
        <!-- note: this is different than previous example, in that  -->
        <!-- the content of the section is in the original place and -->
        <!-- was not moved here; in addition, the section is also    -->
        <!-- specified here, just by its name, so that it gets       -->
        <!-- unlocked only for the site specified in the location.   -->
        <windowsAuthentication/>
      </authentication>
    </security>
  </system.webServer>
</location>

回到上述第三個範例,所有網站中的所有應用程式都會解除鎖定區段, (location path=「」。) 。 檢查主要 < 驗證 > 區段群組是否 (專案外部 <location> 的群組,) 檔案中的上方不包含 < windowsAuthenitcation > 區段。 區段不能同時出現在位置標籤外部和 < 位置路徑=「的相同檔案中 > 。標記;這被視為不正確組態。

若要測試區段是否已鎖定,請在瀏覽器中移至 http://localhost/app

如果區段已鎖定,瀏覽器會顯示錯誤,因為應用層級 <windowsAuthentication> 的web.config檔案有該區段。 這表示web.config嘗試覆寫 <windowsAuthentication> 其層級。 不過,因為該區段現在已在全域層級鎖定,所以web.config檔案中的組態無效。

將位置標籤變更為具有 overrideMode=「Deny」。 這會再次鎖定區段。 試驗其他區段,例如machine.config或根web.config中的 ASP.NET 區段。嘗試在全域層級鎖定它們,並在web.config層級覆寫它們。

工作 2:鎖定特定元素和屬性

建置在上一個工作上,找出 <windowsAuthentication> 標記內的 <location> 區段。 設定位置標籤以解除鎖定區段:overrideMode=「Allow」。 我們只會鎖定區段的特定部分。

enabled 屬性設定為 true,然後藉由設定 lockAttributes=「enabled」 加以鎖定。

這可防止應用層級組態檔變更區段啟用屬性的值 <windowsAuthentication>

如果您想要鎖定其他屬性,請將它們新增至以逗號分隔的 lockAttributes 值,如下列範例所示:

lockAttributes="enabled,attribute1,attribute2"

您也可以使用 「*」 鎖定所有屬性,如下列範例所示:

lockAttributes="*"

區段現在看起來應該如下所示:

<location path="." overrideMode="Allow">   <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" lockAttributes="enabled">          
          <providers>
            <add value="Negotiate" />
            <add value="NTLM" />
          </providers>
        </windowsAuthentication>
      </authentication>
    </security>
  </system.webServer>
</location>

在應用程式的web.config檔案中,嘗試覆寫 區段中的 <windowsAuthentication> 設定。

在瀏覽器中,要求頁面確認您可以覆寫您鎖定的所有設定,在此案例中為 啟用 的屬性。

注意

只要在web.config檔案中指定 屬性會造成組態失敗,即使您在Web.config檔案中設定的屬性的值與ApplicationHost.config檔案中的值相同也一樣。 將鎖定的屬性設定為任何值,會被視為嘗試覆寫屬性,因此會失敗。 (另請注意,屬性與元素不同— 在下一個工作中,您將鎖定 element.)

移除 lockAttributes 屬性。

將 lockElements=「providers」 設定為鎖定 <providers> 區段中的專案。

如果您有其他要鎖定的專案,您可以將它們以逗號分隔,如下所示:

lockElements="providers,element1,element2"

區段現在看起來應該如下所示:

<location path="." overrideMode="Allow">   <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" lockElements="providers">
          <providers>
            <add value="Negotiate" />
            <add value="NTLM" />
          </providers>
        </windowsAuthentication>
      </authentication>
    </security>
  </system.webServer>
</location>

在應用程式Web.config檔案中,藉由設定元素或嘗試加入、移除或清除集合來覆寫 <providers> 元素。

在瀏覽器中,要求頁面並注意錯誤顯示。 在web.config檔案中,覆寫其他專案或屬性,例如 啟用 的屬性。 流覽至頁面,並注意不會顯示任何錯誤。

移除 lockElements 屬性。

工作 3:鎖定特定屬性以外的所有專案

在這項工作中,您將瞭解如何鎖定區段中的所有元素或屬性,但您定義的特定專案除外。 如果您不確定區段具有或未來會有哪些屬性,而且您想要鎖定您明確設定為解除鎖定的屬性以外的所有屬性,這非常有用。

在上一個工作中建置,在位置標籤中找出 區 <windowsAuthentication> 段。

lockAllElementsExceptlockAllAttributesExcept 屬性設定為要鎖定的專案或屬性逗號分隔清單。 例如,區段看起來可能如下所示:

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

或者,如下所示:

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

在此特定區段中,目前沒有任何其他屬性或元素。 如果您想要測試設定 lockAllElementsExceptlockAllAttributesExcept 屬性的效果,請將相同的屬性新增至具有更豐富屬性集的其他區段。

工作 4:鎖定某些集合指示詞

在這項工作中,您會瞭解如何鎖定 <add> 集合上的 和 <remove> 指示詞,以便在應用層級新增組態檔元素,但無法移除。

在上一個工作中建置,在位置標籤中找出 區 <windowsAuthentication> 段。

設定集合中的 <providers>lockElements屬性以移除、清除

完成時,區段看起來如下:

<windowsAuthentication enabled="true" >
  <providers lockElements="remove,clear">
    <add value="Negotiate" />
    <add value="NTLM" />
  </providers>
</windowsAuthentication>

在應用程式的web.config檔案中,建立 <remove> 從集合中移除 NTLM 元素的專案。

完成時,web.config檔案看起來如下:

<configuration>
  <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication>
          <providers>
            <remove value="NTLM" />
          </providers>
        </windowsAuthentication>
      </authentication>
    </security>
  </system.webServer>
</configuration>

在瀏覽器中,要求 http://localhost/app

工作 5:鎖定集合中的特定元素

在此工作中,您會瞭解如何鎖定特定的集合元素。 開發人員仍然可以在較低層級 (應用程式) 層級將元素新增至集合,而且仍然可以從集合中移除非鎖定的專案。 不過,它們無法移除您特別鎖定的專案。 無法清除集合,因為清除表示從集合中移除所有專案。

建置在先前的工作上,找出位置標籤中的 區 <windowsAuthentication> 段。

<providers>在集合中 <add> ,在 NTLM 提供者的 元素中,將lockItem設定為 「true」。

完成時,區段看起來如下:

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

在應用程式web.config檔案中,建立 <remove> 從集合中移除 NTLM 元素的專案。

完成時,Web.config檔案看起來如下:

<configuration>
  <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication>
          <providers>
            <remove value="NTLM" />
          </providers>
       </windowsAuthentication>
      </authentication>
    </security>
  </system.webServer>
</configuration>

在瀏覽器中,要求 http://localhost/app -- 要求失敗。

總結

在本檔中,您已瞭解如何鎖定組態設定。 您可以使用 元素或將標記的lockItem屬性設定為 true, <location> 來鎖定整個區段。 如果您使用 lockAttributeslockElementslockAllAttributesExcept、lockAllElementsExceptlockAllElementsExcept或集合元素上的 lockItem 設定,而且如果您使用集合上的 lockElements 設定來指定特定集合指示詞, <add> (、 <remove><clear>) 。 鎖定可以發生在階層的任何層級,而不只是在ApplicationHost.config中。鎖定會從該層級向下生效。