2012-02-16 60 views
1

在我的Page_Load()的asp.net web應用的方法,我有以下顯示出來:ASP.NET_SessionID cookie不會在請求,但在Page_Load中()

HttpCookie c = Request.Cookies["ASP.NET_SessionId"]; 
if (c != null) { 
    Label1.Text = "Cookie ASP.NET_SessionId sent by client in the request"; 
} 

從我所有的瀏覽器,我清除緩存,cookie等,然後運行應用程序,Label顯示cookie已找到,即使Fiddler中的請求標頭清楚地顯示請求中沒有發送cookie。沒有辦法發送任何cookie,因爲不僅在瀏覽器的內存或硬盤上沒有名爲ASP.NET_SessionID的cookie,而且正如我之前所說的,Fiddler清楚地表明沒有cookie與http請求一起發出。

Fiddler確實顯示http響應有一個Set-Cookie,但這是Page_Load()執行後。

所以,我很困惑,Page_Load()方法如何找到cookie?

感謝您花時間閱讀本文。

+0

也許是會話cookie在web配置改名?請參閱 2012-02-16 15:41:22

回答

0

試試這個insted的

if (Request.Cookies["ASP.NET_SessionId"] != null) { 
    Label1.Text = "Cookie ASP.NET_SessionId sent by client in the request"; 
} 
1

爲HttpCookieCollection的字符串索引使該HttpCookieCollection.Get方法的調用。根據http://msdn.microsoft.com/en-us/library/ezy11xy2(v=vs.110).aspx,如果cookie尚不存在,則Get方法將創建一個具有指定名稱的cookie。

如果指定的cookie不存在,此方法創建一個新的Cookie 使用該名稱。

這個問題已經燒了我之前!

要檢查是否存在cookie的時候沒有自動創建它,如果它沒有,你可以使用類似的語法我所如下圖所示:

bool requestCookieSet = Request.Cookies.AllKeys.Contains(myCookieName); 
相關問題