Loading ...

ASP.NET: Deleting cookie with path created from parent site | CodeAsp.Net

ASP.NET: Deleting cookie with path created from parent site

 /5
0 (0votes)

Today I was facing one issue where I was trying to delete a cookie which was created from parent site with path set to child site. I was having two websites abc.com and abc.com/site1. On the parent site(abc.com) I created the following cookie:

var httpCookie = new HttpCookie("MyCookie");
httpCookie.Path = "/site1";
httpCookie.Domain = ".abc.com";
HttpContext.Current.Response.Cookies.Add(httpCookie);

Cookie was created successfully and domain and path was showing correct in the cookie information in browser console(Chrome). In the child site(abc.com/site1) I was trying to delete the same cookie with the following code:

HttpCookie userCookie = Request.Cookies["MyCookie"];

if (userCookie != null)
{
userCookie.Expires = DateTime.Now.AddDays(-1);
userCookie.Domain = ".abc.com";
userCookie.Path = "/site1";
Response.Cookies.Add(userCookie);
}

The above code was not working as expected.  The cookie was not getting deleted. Then I changed my code to:

HttpCookie userCookie = Request.Cookies["MyCookie"];

if (userCookie != null)
{
userCookie.Expires = DateTime.Now.AddDays(-1);
//userCookie.Domain = ".abc.com";
userCookie.Path = "/site1";
Response.Cookies.Add(userCookie);
}

and the cookie was deleted successfully. This was strange to me. It should have worked as expected since I was specifying the correct domain. But if I remove the domain stuff from the cookie while deleting it it worked!. May be there exists some bug Response.Cookies.Add while specifying domain. I tested the same in these 3 browsers IE9, Chrome, and FF and it worked fine in all three.

 

Comments (no comments yet)

Top Posts