在ASHX中检索cookie值

Céd*_*vin 3 c# asp.net cookies ashx

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

我在页面中设置了一个cookie,我想在我的ashx中检索它.我的cookie总是为空.

我这样保存我的饼干

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

我像这样检索我的cookie

 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
 return new Guid(cookie["siteGuid"]);
Run Code Online (Sandbox Code Playgroud)

好的抱歉这是我的错.我的处理程序在子域上.

小智 6

如果要访问子域中的cookie.您可能需要为cookie分配域名>

Response.Cookies["domain"].Domain = ".somedomain.com";
Run Code Online (Sandbox Code Playgroud)

不要错过域名前的.(Dot).


Chr*_*lor 5

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

它看起来像下面这样

HttpCookie cookie = HttpContext.Current.Request.Cookies["cookieName"];
Run Code Online (Sandbox Code Playgroud)