2

有没有办法在 ASHX Handler 中检索 cookie 值?

我在页面中设置了一个 cookie,我想在我的 ashx 中检索它。我的 cookie 始终为空。

我像这样保存我的cookie

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

我像这样检索我的cookie

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

好吧,对不起,那是我的错。我的处理程序在一个子域上。

4

3 回答 3

6

如果您想跨子域访问 cookie。您可能需要为 cookie 分配域名>

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

不要错过域名前的.(点)。

于 2010-12-08T17:35:29.033 回答
5

您可以访问 Request 对象上的 cookie 集合。

它看起来像下面这样

HttpCookie cookie = HttpContext.Current.Request.Cookies["cookieName"];
于 2010-12-08T16:21:58.327 回答
1

写一个饼干:

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

读取 cookie:

var cookie = (HttpCookie)HttpContext.Current.Request.Cookies["UserName"];
于 2010-12-08T16:26:33.310 回答