IIS 資料夾權限的設定方式


  1. 情境 1 Windows 驗證
    1. NTFS 安全性
      1. 應用程式集區的授權
    2. 資料夾共用
    3. 檔案上傳
  2. 情境 2 匿名驗證
    1. NTFS 安全性
  3. 結論應用
  4. 參考資料

IIS (Internet Information Server) 的權限設定只是幾個小按鈕就能完成,但正確的設定就能避免浪費除錯時間。而在設定時最基本的情境就是要讓網站能夠如期呈現,並且讓對應權限的使用者進行符合權限的動作。

logo

情境 1 Windows 驗證

開發應用系統,使用 Windows 驗證,應用程式會利用 Identity.User 的資訊與資料庫中的權限管理做系統權限的驗證。同時伺服器應用程式所在的資料夾必須支援共用資料夾,如此便可以藉由 UNC 的方式發布編譯後的程式至伺服器。

NTFS 安全性

授權用途 常用授權
過版與程式發布 (Read / Write) Domain\AD Editors Groups
Static Files (Read) Domain\Authenticated Users
應用程式集區 依照集區識別身分來決定

為什麼 Static Files 要給 Domain\Authenticated Users?因為在 Managed Handler 當中有包含 FileAuthorization HTTP Module。會到 Windows Indetity 檢查是否具備 NTFS 權限。若明確授權各個 Windows Identity 有困難,則可以將所有 Windows Identity 都從屬的 Authenticated Users 群組作為安全性授權對性,一次解決。

在 %systemroot%\system32\inetsrv\config\applicationHost.config 當中可以觀察到 HTTP Modules 的註冊:

<system.webServer>
  <modules>
    <add 
      name="FileAuthorization" 
      type="System.Web.Security.FileAuthorizationModule" 
      preCondition="managedHandler" />
  </modules>
</system.webServer>

System.Web/Security/FileAuthorizationModule.cs

private static bool IsUserAllowedToFile(HttpContext context, string fileName) {
    ////////////////////////////////////////////////////////////
    // Step 1: Check if this is WindowsLogin
    // It's not a windows authenticated user: allow access
    if (!IsWindowsIdentity(context)) {
        return true;
    }

    if (fileName == null) {
        fileName = context.Request.PhysicalPathInternal;
    }

    ...
}

可以發現 FileAuthorizationModule 是針對 Windows Identity 進行的檔案安全性檢查,如果非 Windows Identity 的驗證方式,例如基本驗證、匿名驗證等,就不會需要經過這項檢查。而這個檢查是作用在所有試圖對檔案的存取,包括圖片、文件或者是 asp 以及 aspx 的存取都算是。所以在 ASP.NET 當中,如果使用者是以 Windows 驗證,並且在 URL 明確存取 asp 以及 aspx 會需要對該檔案有 NFTS 安全性的讀取權限;但如果是 MVC 的路由方式則不需要 😄

詳細的程式碼可以參考 GitHub

應用程式集區的授權

識別身分 權限需求
Application IdentityPool IIS AppPool\PoolName
Windows User Domain\Authenticated Users

資料夾共用

  • 進階共用移除 Everyone
  • 進階共用加入 Domin\AD Editors groups (Read /Wrtie)

完成設定後系統會比對進階共用與 NTFS 安全性權限的交集,因此一般使用者僅能透過 IIS_IUSRS 進行瀏覽,而屬於 AD Editors Groups 的開發人員可以透過 UNC 存取發布程式的資料夾。

檔案上傳

如果有上傳檔案的需求,則需要另外對上傳的資料夾提供IIS AppPool\PoolName (Write) 的權限,使用者方可藉由IIS AppPool\PoolName 將檔案上傳至伺服器。

情境 2 匿名驗證

僅需要匿名驗證時,則加入 IUSR 並提供 Read 的權限。

權限設定

NTFS 安全性

  • Domain\Authenticated Users(Read)
  • Machine\IUSRS (Read)

如果非網域環境,例如對外網站以 Everyone 取代 Authenticated Users 授權

  • Machine\Everyone (Read)

結論應用

集區使用 Application Pool Identity & Windows 驗證

安全性需要下列:

  • IIS AppPool\PoolName
  • Domain\Authenticated Users (如果有提供 Static Files 或 Web Form 的 aspx)

集區使用 Windows Specific User & Windows 驗證

安全性需要下列:

  • Domain\Authenticated Users (如果有提供 Static Files 包含 web form 的 aspx)
  • Domain\Windows Specific User (ASP.NET MVC Handler 無 Static Files)

集區使用 Application Pool Identity & 匿名驗證

安全性需要下列:

  • Machine\IUSR

參考資料

Secure Content in IIS Through File System ACLs