共用方式為


ASP.NET Core 模組與 IIS 的進階設定配置

注意

這篇文章不是最新的版本。 若要查看最新版本,請參閱本文的.NET 9 版本

警告

此版本的 ASP.NET Core 已不再受支援。 如需詳細資訊,請參閱 .NET 和 .NET Core 支援政策。 目前版本請參閱本文的.NET 9版本。

重要

此資訊涉及一款尚未發行的產品,此產品在正式商業發布之前可能會經過大幅修改。 Microsoft 對此提供的資訊不作任何明示或暗示的保證。

For the current release, see the .NET 9 version of this article.

這篇文章涵蓋 ASP.NET Core 模組和 IIS 的高級配置選項和場景。

修改堆疊大小

僅適用於使用進程內託管模型時。

使用檔案中的web.config十六進位位位元組設定來stackSize設定受控堆疊大小。 默認大小為 0x100000 個字節 (1 MB)。 以下範例將堆疊大小更改為 2 MB(2,097,152 字節),十六進制為 0x200000。

<aspNetCore processPath="dotnet"
    arguments=".\MyApp.dll"
    stdoutLogEnabled="false"
    stdoutLogFile="\\?\%home%\LogFiles\stdout"
    hostingModel="inprocess">
  <handlerSettings>
    <handlerSetting name="stackSize" value="200000" />
  </handlerSettings>
</aspNetCore>

禁止在配置中旋轉

disallowRotationOnConfigChange 設定是為藍綠場景設計的,在此情境下,對全域設定的變更不應導致所有網站重新啟動。 當此旗標為真時,只有與網站本身相關的變更才會導致它重新啟用。 例如,如果站點的web.config發生變更,或者有與IIS角度中站點路徑相關的內容變更,該站點就會進行重啟。 但是,對applicationHost.config的一般變更不會導致應用程式重啟。 接下來的範例將此設定設為 true:

<aspNetCore processPath="dotnet"
    arguments=".\MyApp.dll"
    stdoutLogEnabled="false"
    stdoutLogFile="\\?\%home%\LogFiles\stdout"
    hostingModel="inprocess">
  <handlerSettings>
    <handlerSetting name="disallowRotationOnConfigChange" value="true" />
  </handlerSettings>
</aspNetCore>

此設定對應於 API ApplicationPoolRecycling.DisallowRotationOnConfigChange

減少應用程式重新啟動時發生 503 錯誤的可能性

預設情況下,IIS 被通知回收或關閉後,ANCM 與受管伺服器之間會有一秒的延遲,然後才會通知受管伺服器開始關閉。 延遲可以通過 ANCM_shutdownDelay 環境變數或透過設定 shutdownDelay 處理程序設置來配置。 Both values are in milliseconds. 延遲主要是為了降低發生競合狀況的可能性。

  • IIS尚未開始排隊將請求送往新應用程式。
  • ANCM starts rejecting new requests that come into the old app.

此設定並不意味著進入的請求會因此被延遲。 設定顯示舊應用程式實例將在超時發生後開始關閉。 Slower machines or machines with heavier CPU usage might need to adjust this value to reduce 503 likelihood.

The following example sets the delay to 5 seconds:

<aspNetCore processPath="dotnet"
    arguments=".\MyApp.dll"
    stdoutLogEnabled="false"
    stdoutLogFile="\\?\%home%\LogFiles\stdout"
    hostingModel="inprocess">
  <handlerSettings>
    <handlerSetting name="shutdownDelay" value="5000" />
  </handlerSettings>
</aspNetCore>

代理配置使用 HTTP 協定和配對代碼

僅適用於進程外託管。

ASP.NET Core 模組與Kestrel之間所建立的代理使用 HTTP 協定。 在伺服器外的位置,模組與Kestrel之間的流量沒有竊聽風險。

使用配對令牌來保證Kestrel接收到的請求是由IIS代理的,而不是來自其他來源。 模組會建立配對憑證並將其設置為環境變數(ASPNETCORE_TOKEN)。 配對令牌也會在每個代理請求的標頭中設置(MS-ASPNETCORE-TOKEN)。 IIS 中介軟體會檢查每個接收到的請求,以確認配對令牌標頭值是否與環境變數值相符。 如果代幣值不匹配,則請求會被記錄並拒絕。 配對令牌環境變數及模組與Kestrel之間的流量無法從伺服器外的位置存取。 在不知道配對令牌值的情況下,網路攻擊者無法提交繞過 IIS 中間件檢查的請求。

ASP.NET Core Module with an IIS Shared Configuration

The ASP.NET Core Module installer runs with the privileges of the TrustedInstaller account. Because the local system account doesn't have modify permission for the share path used by the IIS Shared Configuration, the installer throws an access denied error when attempting to configure the module settings in the applicationHost.config file on the share.

When using an IIS Shared Configuration on the same machine as the IIS installation, run the ASP.NET Core Hosting Bundle installer with the OPT_NO_SHARED_CONFIG_CHECK parameter set to 1:

dotnet-hosting-{VERSION}.exe OPT_NO_SHARED_CONFIG_CHECK=1

When the path to the shared configuration isn't on the same machine as the IIS installation, follow these steps:

  1. 停用 IIS 共用設定。
  2. 執行安裝程式
  3. 將更新的applicationHost.config文件匯出到文件共享。
  4. 重新啟用 IIS 共用設定。

資料保護

ASP.NET Core 資料保護堆疊 被多個 ASP.NET Core 中介軟體 使用,包含用於身份驗證的中介軟體。 即使使用者程式碼未調用資料保護 API,也應透過部署指令碼或在使用者程式碼中配置資料保護,以建立持久的加密密鑰存儲。 如果未配置資料保護,則密鑰會保留在記憶體中,並在應用程式重啟時被丟棄。

如果應用程式重新啟動時,資料保護金鑰環儲存在記憶體中:

  • 所有基於 cookie 的驗證權杖均作廢。
  • 使用者需要在下次請求時重新登入。
  • 任何使用密鑰環保護的數據將無法再被解密。 這可能包括CSRF 標記ASP.NET Core MVC TempData cookies

要在 IIS 下配置資料保護以保持密鑰環,使用以下方法之一:

  • 創建資料保護註冊表鍵

    ASP.NET Core 應用程式使用的資料保護金鑰儲存在應用程式外部的註冊表中。 欲持續某個應用程式的金鑰,請為應用程式池建立註冊表鍵。

    針對獨立的、非網頁伺服器群組的 IIS 安裝,您可以對每個用於 ASP.NET Core 應用程式的應用程式集區使用 Data Protection Provision-AutoGenKeys.ps1 PowerShell 腳本。 這個腳本會在 HKLM 註冊表中創建一個註冊表鍵,該鍵僅對應用程式應用程式池的工作程序帳戶可訪問。 Keys are encrypted at rest using DPAPI with a machine-wide key.

    在網頁伺服器陣列的情境中,可以將應用程式配置為使用 UNC 路徑來儲存其數據保護金鑰環。 預設情況下,這些金鑰不會被加密。 請確保網路共享的檔案權限僅限於應用程式執行所使用的 Windows 帳戶。 An X509 certificate can be used to protect keys at rest. 考慮一種機制來允許用戶上傳證書。 將憑證放入使用者的信任憑證存儲中,並確保它們在使用者的應用程式運行的所有機器上都可用。 如需更多資訊,請參見ASP.NET Core 資料保護設定

  • 配置 IIS 應用程式集區以載入使用者設定檔

    This setting is in the Process Model section under the Advanced Settings for the app pool. Set Load User Profile to True. When set to True, keys are stored in the user profile directory and protected using DPAPI with a key specific to the user account. 金鑰匙會被持久化存到 %LOCALAPPDATA%/ASP.NET/DataProtection-Keys 資料夾。

    必須同時啟用應用程式集區的setProfileEnvironment屬性。 The default value of setProfileEnvironment is true. 在某些情境下(例如,Windows作業系統),setProfileEnvironment 被設置為 false。 如果金鑰未如預期儲存在使用者個人資料目錄中:

    1. Navigate to the %windir%/system32/inetsrv/config folder.
    2. 開啟applicationHost.config文件。
    3. 找出 <system.applicationHost><applicationPools><applicationPoolDefaults><processModel> 元素。
    4. Confirm that the setProfileEnvironment attribute isn't present, which defaults the value to true, or explicitly set the attribute's value to true.
  • 使用檔案系統作為金鑰環存儲

    調整應用程式代碼以使用檔案系統作為密鑰環存儲。 Use an X509 certificate to protect the key ring and ensure the certificate is a trusted certificate. 若憑證是自行簽署的,請將憑證放入受信任的根存儲區中。

    When using IIS in a web farm:

    • Use a file share that all machines can access.
    • Deploy an X509 certificate to each machine. 在程式碼中設定資料保護
  • 設定全機資料保護政策

    資料保護系統對於設定所有使用資料保護API的應用程式的預設整機政策的支援有限。 如需更多資訊,請參閱 ASP.NET Core Data Protection Overview

IIS configuration

Windows Server operating systems

啟用 Web Server (IIS) 伺服器角色並建立角色服務。

  1. 使用新增角色和功能精靈,從管理功能表或伺服器管理員中的連結進行操作。 在伺服器角色步驟中,勾選Web 伺服器 (IIS)的方框。

    在選擇伺服器角色步驟中選擇了 Web 伺服器 IIS 角色。

  2. 在完成功能步驟後,角色服務步驟會為網路伺服器 (IIS) 加載。 選擇所需的 IIS 角色服務,或接受提供的預設角色服務。

    在選擇角色服務步驟中,預設的角色服務已被選取。

    Windows Authentication (Optional)
    若要啟用 Windows 驗證,請展開以下節點:Web Server>Security。 選取Windows 認證功能。 如需更多資訊,請參閱 Windows 身分驗證<windowsAuthentication>配置 Windows 身分驗證

    WebSockets (Optional)
    ASP.NET Core 1.1 或更新版本支援 WebSockets。 要啟用WebSockets,請展開以下節點:Web伺服器>應用程式開發。 請選擇 WebSocket Protocol 功能。 欲了解更多信息,請參閱 WebSockets

  3. 通過確認步驟安裝網路伺服器角色和服務。 安裝Web 伺服器 (IIS)角色後,不需要重新啟動伺服器/IIS。

Windows desktop operating systems

啟用IIS 管理控制檯全球資訊網服務

  1. 請前往控制台>程式>程式和功能>啟用或關閉 Windows 功能(畫面的左側)。

  2. Open the Internet Information Services node. Open the Web Management Tools node.

  3. 請勾選IIS 管理控制台

  4. 勾選 World Wide Web Services 的框。

  5. 使用世界資訊網服務的預設功能,或自訂IIS功能。

    Windows Authentication (Optional)
    To enable Windows Authentication, expand the following nodes: World Wide Web Services>Security. 請選取Windows 驗證功能。 For more information, see Windows Authentication <windowsAuthentication> and Configure Windows authentication.

    WebSockets (Optional)
    WebSockets 支援從 ASP.NET Core 1.1 或更新版本開始。 若要啟用 WebSockets,請展開以下節點:World Wide Web Services>應用程式開發功能。 Select the WebSocket Protocol feature. 如需更多資訊,請參見WebSockets

  6. 如果 IIS 安裝需要重新啟動,請重新啟動系統。

在 Windows 功能中選擇了 IIS 管理控制台和萬維網服務。

Virtual Directories

IIS Virtual Directories aren't supported with ASP.NET Core apps. 應用程式可以以子應用程式的形式托管。

Sub-applications

ASP.NET Core 應用程式可以作為 IIS 子應用程式(子應用)來託管。 The sub-app's path becomes part of the root app's URL.

Static asset links within the sub-app should use tilde-slash (~/) notation in MVC and Razor Pages. 波浪形斜線符號標記會觸發 標籤輔助工具,將子應用程式的路徑基底添加到所呈現的相對鏈接。 For a sub-app at /subapp_path, an image linked with src="~/image.png" is rendered as src="/subapp_path/image.png". 根應用程式的靜態文件中介軟體不處理靜態文件請求。 該請求由子應用程序的靜態文件中間件進行處理。

注意

Razor components (.razor) shouldn't use tilde-slash notation. 如需詳細資訊,請參閱 ASP.NET Core Blazor 應用程式基底路徑

如果靜態資產的src屬性被設定為絕對路徑 (例如 src="/image.png"),則連結將會在沒有子應用程式的路徑基底的情況下呈現。 根應用程式的靜態文件中介軟體嘗試從根應用程式的網路根目錄提供資產,結果會導致404 - 找不到的回應,除非該靜態資產可以從根應用程式獲得。

若要將 ASP.NET Core 應用程式作為子應用程式托管在另一個 ASP.NET Core 應用程式下:

  1. 為子應用程式建立一個應用程式池。 將.NET CLR版本設置為無托管代碼,因為.NET Core的核心公共語言運行時(CoreCLR)被啟動以便在工作進程中托管應用程式,而不是桌面CLR(.NET CLR)。

  2. Add the root site in IIS Manager with the sub-app in a folder under the root site.

  3. 於 IIS 管理員中的子應用程式資料夾上按下滑鼠右鍵,然後選擇 轉換為應用程式

  4. In the Add Application dialog, use the Select button for the Application Pool to assign the app pool that you created for the sub-app. Select OK.

使用進程內部託管模型時,為子應用程序分配獨立的應用程序池是必須的。

若需了解有關程序內宿主模型和配置 ASP.NET Core 模組的詳細資訊,請參閱IIS 的 ASP.NET Core 模組 (ANCM)

應用程式集區

應用程式池隔離是由託管模型決定的:

  • 處理中宿主:應用程式必須在不同的應用程式池中執行。
  • Out-of-process hosting: We recommend isolating the apps from each other by running each app in its own app pool.

IIS 的 新增網站 對話框預設為每個應用程式配置一個應用程式集區。 When a Site name is provided, the text is automatically transferred to the Application pool textbox. A new app pool is created using the site name when the site is added.

應用程式集區 Identity

應用程式集區身分識別帳戶可讓應用程式以唯一帳戶執行,而不需要建立和管理網域或本機帳戶。 On IIS 8.0 or later, the IIS Admin Worker Process (WAS) creates a virtual account with the name of the new app pool and runs the app pool's worker processes under this account by default. 請在 IIS 管理主控台中的應用程式集區的進階設定中,確保Identity設定為使用ApplicationPoolIdentity

Application pool advanced settings dialog

IIS 管理程序在 Windows 安全系統中以應用程式集區的名稱創建一個安全識別碼。 您可以使用此身分識別來保護資源。 不過,此身分識別不是真正的用戶帳戶,也不會顯示在 Windows 使用者管理控制台中。

If the IIS worker process requires elevated access to the app, modify the Access Control List (ACL) for the directory containing the app:

  1. 打開Windows資源管理器並導航到該目錄。

  2. 在目錄上按右鍵,然後選擇屬性

  3. Security標籤下,選擇Edit按鈕,然後選擇Add按鈕。

  4. 選擇地點按鈕,並確保系統處於選中狀態。

  5. Enter IIS AppPool\{APP POOL NAME} format, where the placeholder {APP POOL NAME} is the app pool name, in Enter the object names to select area. Select the Check Names button. 針對DefaultAppPool檢查使用IIS AppPool\DefaultAppPool的名稱。 When the Check Names button is selected, a value of DefaultAppPool is indicated in the object names area. 無法直接在物件名稱區域輸入應用程式池的名稱。 請使用 IIS AppPool\{APP POOL NAME} 格式,其中 {APP POOL NAME} 佔位符是應用程式池名稱,當檢查物件名稱時。

    應用程式資料夾的選擇使用者或群組對話框:在選擇

  6. Select OK.

    應用程式資料夾的「選取使用者或群組」對話框:選擇「檢查名稱」後,物件名稱區域將顯示物件名稱「DefaultAppPool」。

  7. 預設應授予讀取和執行的權限。 視需要提供額外的權限。

可以在命令提示字元中使用 ICACLS 工具來授予訪問權限。 以 DefaultAppPool 為例,以下命令用來授予 MyWebApp 資料夾、子資料夾及檔案讀取和執行權限。

ICACLS C:\sites\MyWebApp /grant "IIS AppPool\DefaultAppPool:(OI)(CI)RX"

如需更多資訊,請參閱icacls主題。

HTTP/2 支援

HTTP/2 在以下 IIS 部署情境中支援 ASP.NET Core:

  • 處理中
    • Windows Server 2016/Windows 10 或更新版本;IIS 10 或更新版本
    • TLS 1.2 或更高版本的連接
  • 程序外執行
    • Windows Server 2016/Windows 10 或更新版本;IIS 10 或更新版本
    • 公開通用的邊緣伺服器連線使用 HTTP/2,但反向代理連接到Kestrel 伺服器則使用 HTTP/1.1。
    • TLS 1.2 或更高版本的連接

當建立 HTTP/2 連線時,HttpRequest.Protocol 會在進行中的部署中報告 HTTP/2。 在建立 HTTP/2 連接時,針對外部部署,HttpRequest.Protocol 報告 HTTP/1.1

如需有關處理中和處理外代管模型的更多資訊,請參閱 ASP.NET Core 模組 (ANCM) 適用於 IIS

預設啟用 HTTP/2。 Connections fall back to HTTP/1.1 if an HTTP/2 connection isn't established. 如需更多有關 IIS 部署的 HTTP/2 設定資訊,請參閱 HTTP/2 on IIS

CORS 預檢請求

此部分僅適用於以 .NET Framework 為目標的 ASP.NET Core 應用程式。

對於一個以 .NET Framework 為目標的 ASP.NET Core 應用程式,OPTIONS 請求在 IIS 中預設不會傳遞給應用程式。 To learn how to configure the app's IIS handlers in web.config to pass OPTIONS requests, see Enable cross-origin requests in ASP.NET Web API 2: How CORS Works.

Application Initialization Module and Idle Timeout

When hosted in IIS by the ASP.NET Core Module version 2:

應用程式初始化模組

適用於內進程和外進程託管的應用程式。

IIS 應用程式初始化 是 IIS 的一項功能,當應用程式集區啟動或重新循環時,它會向應用程式發送一個 HTTP 請求。 The request triggers the app to start. By default, IIS issues a request to the app's root URL (/) to initialize the app (see the additional resources for more details on configuration).

確認 IIS 啟用應用程式初始化角色功能:

On Windows 7 or later desktop systems when using IIS locally:

  1. Navigate to Control Panel>Programs>Programs and Features>Turn Windows features on or off (left side of the screen).
  2. Open Internet Information Services>World Wide Web Services>Application Development Features.
  3. Select the checkbox for Application Initialization.

On Windows Server 2008 R2 or later:

  1. 開啟新增角色及功能精靈
  2. In the Select role services panel, open the Application Development node.
  3. Select the checkbox for Application Initialization.

Use either of the following approaches to enable the Application Initialization Module for the site:

  • 使用 IIS 管理員:

    1. Select Application Pools in the Connections panel.
    2. Right-click the app's app pool in the list and select Advanced Settings.
    3. The default Start Mode is OnDemand. 將啟動模式設定為AlwaysRunning。 Select OK.
    4. 連線面板中打開網站節點。
    5. 右鍵點擊應用程式並選擇管理網站>進階設定
    6. 預設的預載啟用設定是False。 將 啟用預加載 設置為 True。 Select OK.
  • Using web.config, add the <applicationInitialization> element with doAppInitAfterRestart set to true to the <system.webServer> elements in the app's web.config file:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <applicationInitialization doAppInitAfterRestart="true" />
        </system.webServer>
      </location>
    </configuration>
    

Idle Timeout

僅適用於在進程中託管的應用程式。

若要防止應用程式閒置,請使用 IIS 管理員設定應用程式集區的閒置逾時時間。

  1. 連線面板中選擇應用程式集區
  2. 在清單中右鍵點擊應用程式的應用程式池,然後選擇進階設定
  3. 預設的閒置超時(分鐘)20分鐘。 設定閒置時間限制(分鐘)0(零)。 Select OK.
  4. 回收工作處理程序。

要防止托管於離處理程序的應用程式發生超時情況,可以使用以下其中一種方法:

應用程式初始化模組和閒置超時附加資源

模組、模式和配置檔案位置

模組

IIS (x86/amd64):

  • %windir%\System32\inetsrv\aspnetcore.dll

  • %windir%\SysWOW64\inetsrv\aspnetcore.dll

  • %ProgramFiles%\IIS\Asp.Net Core Module\V2\aspnetcorev2.dll

  • %ProgramFiles(x86)%\IIS\Asp.Net Core Module\V2\aspnetcorev2.dll

IIS Express (x86/amd64):

  • %ProgramFiles%\IIS Express\aspnetcore.dll

  • %ProgramFiles(x86)%\IIS Express\aspnetcore.dll

  • %ProgramFiles%\IIS Express\Asp.Net Core Module\V2\aspnetcorev2.dll

  • %ProgramFiles(x86)%\IIS Express\Asp.Net Core Module\V2\aspnetcorev2.dll

Schema

IIS

  • %windir%\System32\inetsrv\config\schema\aspnetcore_schema.xml

  • %windir%\System32\inetsrv\config\schema\aspnetcore_schema_v2.xml

IIS Express

  • %ProgramFiles%\IIS Express\config\schema\aspnetcore_schema.xml

  • %ProgramFiles%\IIS Express\config\schema\aspnetcore_schema_v2.xml

設定

IIS

  • %windir%\System32\inetsrv\config\applicationHost.config

IIS Express

  • Visual Studio: {APPLICATION ROOT}\.vs\config\applicationHost.config

  • iisexpress.exe CLI: %USERPROFILE%\Documents\IISExpress\config\applicationhost.config

可以在applicationHost.config檔案中搜尋aspnetcore以找到這些檔案。

在使用 Visual Studio 發佈時安裝 Web Deploy

當使用Web Deploy將應用程式部署到伺服器時,請在伺服器上安裝最新版的 Web Deploy。 若要安裝 Web Deploy,請查看 IIS 下載:Web Deploy

Create the IIS site

  1. On the hosting system, create a folder to contain the app's published folders and files. 在接下來的步驟中,資料夾的路徑會提供給 IIS,作為應用程式的實體路徑。 如需了解应用程式的部署文件夹和档案布局的详细资讯,请参阅 ASP.NET Core 目录结构

  2. 在 IIS 管理器中,開啟 連線 面板中的伺服器節點。 右鍵點擊Sites資料夾。 Select Add Website from the contextual menu.

  3. 提供站點名稱並將實體路徑設置為應用程式的部署資料夾。 提供Binding配置並選擇OK以創建網站。

    在新增網站步驟中,提供網站名稱、實體路徑和主機名稱。

    警告

    不應使用頂層萬用字元綁定(http://*:80/http://+:80)。 頂層萬用字元綁定可能會使您的應用程式面臨安全漏洞。 這適用於強弱通配符。 使用明確的主機名稱而不是通配符。 如果您能控制整個父網域,子網域萬用字元綁定(例如,*.mysub.com)就不會有此安全風險(相比之下,*.com 則容易受到攻擊)。 請參閱 RFC 9110: HTTP 語義學(第 7.2 節:主機與 :authority) 以獲取更多信息。

  4. 在伺服器的節點下,選擇應用程式集區

  5. Right-click the site's app pool and select Basic Settings from the contextual menu.

  6. In the Edit Application Pool window, set the .NET CLR version to No Managed Code:

    Set No Managed Code for the .NET CLR version.

    ASP.NET Core 在獨立的程序中運行並管理運行時。 ASP.NET Core doesn't rely on loading the desktop CLR (.NET CLR). .NET Core 的核心通用語言執行平台 (CoreCLR) 被啟動以在工作進程中托管應用程式。 Setting the .NET CLR version to No Managed Code is optional but recommended.

    • For a 32-bit (x86) self-contained deployment published with a 32-bit SDK that uses the in-process hosting model, enable the Application Pool for 32-bit. In IIS Manager, navigate to Application Pools in the Connections sidebar. Select the app's Application Pool. In the Actions sidebar, select Advanced Settings. 設置啟用32位應用程式True

    • 為使用進程內宿主模式的 64 位元 (x64) 自包含部署,請禁用 32 位元 (x86) 處理程序的應用程式集區。 In IIS Manager, navigate to Application Pools in the Connections sidebar. Select the app's Application Pool. In the Actions sidebar, select Advanced Settings. Set Enable 32-Bit Applications to False.

  7. 確認進程模型識別具有適當的許可權。

    如果應用程式集區的預設身分識別(進程模型>Identity)從 ApplicationPoolIdentity 變更為另一個身分識別,請確認新身分識別具有存取應用程式資料夾、資料庫和其他必要資源的必要許可權。 例如,應用程式集需要對應用程式讀寫文件的資料夾擁有讀取和寫入訪問權限。

Windows 驗證配置(可選)
欲了解更多資訊,請參閱Configure Windows authentication

陰影複製

Shadow copying app assemblies to the ASP.NET Core Module (ANCM) for IIS can provide a better end user experience than stopping the app by deploying an app offline file.

在 Windows 上執行的 ASP.NET Core 應用程式,其二進位檔案會被鎖定,因此無法被修改或替換。 陰影複製允許應用程式的組件在運行中通過製作組件的副本進行更新。

快照複製並非用於實現零停機部署,因此可能會出現 IIS 仍然回收應用程式的情況,並且部分請求可能會收到「503 服務暫時無法使用」的回應。 我們建議使用像 藍綠部署Azure 部署插槽 的模式,以實現零停機部署。 使用陰影複製可以減少部署時的停機時間,但無法完全消除它。

Shadow copying is enabled by customizing the ANCM handler settings in web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".logsstdout">
      <handlerSettings>
        <handlerSetting name="enableShadowCopy" value="true" />
        <!-- Ensure that the IIS ApplicationPool identity has permission to this directory -->
        <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" />
      </handlerSettings>
    </aspNetCore>
  </system.webServer>
</configuration>