ASP.NET 支援跨多個應用程序(甚至跨服務器場)共享 Forms 認證票據。本文以雙站點實戰為例,詳細講解 web.config 關鍵配置、machineKey 生成與常見坑點。
https://localhost/test/(受保護資源)https://localhost:81/(含登錄頁 Login.aspx)<authentication mode="Forms">
<forms loginUrl="https://localhost:81/Login.aspx"
path="/"
protection="All"
defaultUrl="https://localhost:81/Default.aspx"
timeout="40"
name=".DeESoft" />
</authentication>
<machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE"
decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F"
validation="SHA1" />
<authorization>
<deny users="?" />
</authorization>
domain=".example.com"。推薦使用 ASP.NET Machine Key Generator 生成符合規範的金鑰,避免手工輸入錯誤。
defaultUrl 與 loginUrl 是否可訪問。machineKey 完全一致、應用程序池未回收。跨應用 Forms 認證的核心在於「同名 Cookie + 同金鑰 + 同保護級別」。生產環境務必將 machineKey 寫入配置文件,並透過負載均衡器 Session Affinity 或共享 Session State 進一步保障穩定性。