2010-12-08 79 views
2

有沒有辦法在ASHX Handler中檢索cookie值?在ASHX中檢索cookie值

我在頁面中設置了一個cookie,我想在我的ashx中檢索它。我的cookie始終爲空。

保存我的餅乾這樣

HttpCookie tokenCookie = new HttpCookie(cookieName); 
tokenCookie.Values["siteGuid"] = authenticationInfo.SiteGuid.ToString(); 
    HttpContext.Current.Response.Cookies.Add(tokenCookie); 

我找回我的餅乾這樣

HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; 
return new Guid(cookie["siteGuid"]); 

好抱歉,是我的錯。我的處理程序位於子域中。

回答

5

HttpContext.Current.Response.Cookies.Add("UserName"); 

讀一個cookie -域。您可能需要爲餅乾分配域名>

Response.Cookies["domain"].Domain = ".somedomain.com"; 

在域名前千萬不要錯過。(點)。

4

您可以訪問Request對象上的Cookie集合。

它看起來像下面

HttpCookie cookie = HttpContext.Current.Request.Cookies["cookieName"]; 
0

寫一個cookie:如果你想跨越子訪問餅乾

var cookie = (HttpCookie)HttpContext.Current.Request.Cookies["UserName"];