Web.config 是 ASP.NET 應用程式的核心設定檔,採 XML 格式定義驗證、授權、Session、連線字串等行為。理解其階層繼承關係,有助於在多層目錄結構中精準控制設定生效範圍。
%Windir%\Microsoft.NET\Framework[版本]\CONFIG\,定義所有 ASP.NET 應用程式的預設值。不建議直接修改,避免影響同台伺服器其他應用。<!-- 根目錄 Web.config -->
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="30" />
</authentication>
</system.web>
<!-- 子目錄 Admin/Web.config -->
<system.web>
<!-- 覆寫登入頁與逾時 -->
<authentication mode="Forms">
<forms loginUrl="~/Admin/Login.aspx" timeout="60" />
</authentication>
<!-- 新增授權規則,僅允許 Administrators 角色 -->
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
</system.web>
<authentication>, <authorization><pages>, <compilation>, <httpModules>, <httpHandlers><connectionStrings>, <appSettings>configSections在父層使用 <location allowOverride="false"> 或 <section allowOverride="false" /> 可禁止子目錄覆寫特定設定,確保安全性關鍵設定不被竄改。
configProtectionProvider 加密。