Asp.net-Session

如何在 ASP.NET 會話 Cookie 上設置安全標誌?

  • September 18, 2009

如何在 ASP.NET 會話 Cookie 上設置安全標誌,使其僅通過 HTTPS 傳輸,而不會通過普通 HTTP 傳輸?

有兩種方法,一個httpCookies元素web.config允許您打開requireSSL它只傳輸所有 cookie,包括僅在 SSL 中的會話以及在表單身份驗證中,但是如果您在 httpcookies 上打開 SSL,您還必須在表單配置中打開它。

為清楚起見進行編輯: 將其放入<system.web>

<httpCookies requireSSL="true" />

<system.web>元素中,添加以下元素:

<httpCookies requireSSL="true" />

但是,如果您的塊<forms>中有一個元素system.web\authentication,那麼這將覆蓋 中的設置httpCookies,將其設置回預設值false

在這種情況下,您還需要將requireSSL="true"屬性添加到表單元素。

所以你最終會得到:

<system.web>
   <authentication mode="Forms">
       <forms requireSSL="true">
           <!-- forms content -->
       </forms>
   </authentication>
</system.web>

有關這些元素的 MSDN 文件,請參見此處此處

引用自:https://stackoverflow.com/questions/1442863