2026年1月16日 星期五

[研究]IIS 10.0 GCB,實測 Web.Config 多甚麼 - forms cookieless="UseCookies"

[研究]IIS 10.0 GCB,實測 Web.Config 多甚麼 - forms cookieless="UseCookies"

2025-01-16

IIS 10.0 套用 GCB 後,有些會變更現有 Web.Config 設定,要回頭改 Source Code,否則下次 Deploy 後,GCB 設定會被覆蓋消失。

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20.2

********************************************************************************

兩個不同系統套用 IIS 10.0 GCB 後 Web.Config 

<authentication mode="None">
            <forms cookieless="UseCookies" />
        </authentication>


<authentication mode="Windows">
            <forms cookieless="UseCookies" requireSSL="true" />
        </authentication>


相關 No.9、No.18,分別

IIS管理員\伺服器\站台\網站\管理\設定編輯器\動作\開啟功能\區段\system.web\authentication\forms\cookieless

IIS管理員\伺服器\站台\網站\管理\設定編輯器\動作\開啟功能\區段\system.web\sessionState\cookieless

No.9 預設「UseDeviceProfile」,GCB建議 UseCookies
No.18 預設值和GCB建議 UseCookies,不會寫 Web.Config

********************************************************************************
No.9  套用後

<configuration>
  <system.web>
    <compilation targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" />
        <machineKey validation="HMACSHA256" />
        <authentication>
            <forms cookieless="UseCookies" />
        </authentication>
  </system.web>
  </configuration>

********************************************************************************

項次TWGCB-ID類別原則設定名稱說明設定位置設定路徑GCB設定值
9TWGCB-04-014-0009設定驗證與授權表單驗證Cookie 模式
  • 這項原則設定決定表單驗證是否使用 Cookie
  • 表單驗證 Cookie 模式選項如下:
    1. UseUri:不使用 Cookie
    2. UseCookies:不論裝置為何,永遠使用 Cookie
    3. AutoDetect:如果裝置設定檔支援 Cookie,則使用 Cookie。否則,不使用 Cookie。針對已知支援 Cookie 之桌面瀏覽器,ASP.NET 會進行檢查以判斷是否啟用 Cookie
    4. UseDeviceProfile:如果裝置設定檔支援 Cookie,則使用Cookie。否則,不使用Cookie。ASP.NET 不會進行檢查來判斷是否要在支援 Cookie之裝置上啟用 Cookie
  • 使用者通過身分驗證登入站台後,表單驗證會在 Cookie 中維護一份驗證資訊,讓已通過驗證的使用者不需要對每個要求都輸入帳號密碼,當 Cookie 過期或找不到有效之 Cookie 時,使用者將被重新導向至指定登入頁面
  • 使用 Cookie 管理使用者連線狀態,可藉由防止 ASP.NET 將Session 資訊透過 URL 傳送,以避免 Session ID 在代理伺服器紀錄檔或瀏覽歷程紀錄中被找到,降低Session 遭竊取之風險
  • 預設設定為「UseDeviceProfile」
站台IIS 管理員\伺服器\站台\網站\管理\設定編輯器\動作\開啟功能\區段\system.web\authentication\forms\cookielessUseCookies

下圖,cookieless預設值是 UseDeviceProfile

改成 UseCookies後,套用,Web.Config 結果如下



[研究]GCB,IIS,「表單驗證Cookie 模式」從預設值 UseDeviceProfile 改為 GCB 建議值 UseCookies 的影響?
主要影響老舊瀏覽器

[研究]GCB,IIS,「表單驗證Cookie 模式」從預設值 UseDeviceProfile 改為 GCB 建議值 UseCookies後那些套件功能會異常?
https://shaurong.blogspot.com/2025/11/gcbiiscookie-usedeviceprofile-gcb.html
Microsoft.AspNet.FriendlyUrls、ASP.NET Identity、Microsoft.Owin.Security.Cookies、jQuery、Bootstrap.Datepicker、js-cookie、vue 可能異常。

實際檢視某 ASP.NET WebForm 真實系統,該網站cookieless設定值是 UseDeviceProfile。
GCB建議是 UseCookies。
"可能" 會導致某些套件無法正常運作。

********************************************************************************



<authentication>的mode值說明
None不指定驗證。
Windows指定 Windows 當做驗證模式。 當使用網際網路資訊服務 (IIS) 驗證方法 (基本、摘要、整合式 Windows (NTLM/Kerberos) 或憑證) 時,會套用這個模式。
Passport指定 Microsoft Passport 當做驗證模式。
Forms將 ASP.NET 表單式驗證指定為驗證模式。

效果表
<authentication mode><forms> 標籤是否有效使用情境行為 / 備註建議環境設定
Forms (預設 <authentication> 未指定 mode 也是 Forms)✅ 有效傳統 WebForm 登入系統使用 <forms> 的 cookie 或 URL 保存驗證票 (FormsAuthenticationTicket);支援 cookielessloginUrltimeout 等屬性開發 / 測試 / 正式:如果使用自訂登入系統(Forms Authentication)
Windows❌ 無效內網 / AD 驗證IIS 控制 Windows 認證(NTLM / Kerberos);忽略 <forms> 設定;登入使用者由 Windows 帳號提供開發 / 測試內網可用;正式機公開網不建議
None❌ 無效不使用 ASP.NET 原生認證完全不啟用 FormsAuthenticationModule;若使用 Microsoft Identity (OWIN) 或其他自訂認證,需要 mode="None"開發 / 測試可快速跳過登入;正式機使用 Microsoft Identity/Owin cookie


實際測試套用了,目前沒看到問題。

(完)

相關

沒有留言:

張貼留言