Search code examples
c#asp.netcookiesashx

Retrieve cookie values in ASHX


It's there a way to retrieve cookie value in ASHX Handler ?

I set a cookie in a page, and I want to retrieve it in my ashx. My cookie is always null.

I save my cookie like this

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

I retrieve my cookie like this

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

Ok sorry that was my fault. My handler was on a sub domain.


Solution

  • If you want to access the cookies across the sub-domain. You might need to assign the domain name for the cookies>

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

    Don't miss the .(Dot) before the domain name.