ASP MVC Cookies not persisting ASP MVC Cookies not persisting asp.net asp.net

ASP MVC Cookies not persisting


The problem lies in following code:

if (System.Web.HttpContext.Current.Response.Cookies["CountryPreference"] == null)

When you try to check existence of a cookie using Response object rather than Request, ASP.net automatically creates a cookie.

Check this detailed post here: http://chwe.at/blog/post/2009/01/26/Done28099t-use-ResponseCookiesstring-to-check-if-a-cookie-exists!.aspx


Quote from the article in case the link goes down again ....

The short explanation, if you don’t like to read the entire story

If you use code like “if (Response.Cookies[“mycookie”] != null) { … }”, ASP.Net automatically generates a new cookie with the name “mycookie” in the background and overwrites your old cookie! Always use the Request.Cookies-Collection to read cookies!

[ More detail in the article ]


In resume, don't use "Response" to read cookies, use "Request".