If you are working on a site that doesn’t have a proper TLD (i.e. http://localhost:8013) you will notice that setting cookies on the browser doesn’t seem to work the way it should. Sometimes your cookie, which works perfectly fine on a site with a TLD, will not show up on your local site.
For example, this code will work perfectly fine on a site with a TLD but not on your local site.
newCookie = New HttpCookie(ConfigurationManager.AppSettings("YOUR_SPECIAL_COOKIE_NAME")) newCookie.Domain = ConfigurationManager.AppSettings("BASE_DOMAIN") HttpContext.Current.Response.Cookies.Add(newCookie)
At this point you might be thinking, ‘I did everything right, where did my cookie go?‘.
This comes down to how the browser handles setting cookies that have a specified domain. The browser prepends a ‘.’ (that’s a dot) to the beginning of any domain specified for a cookie. On normal domains this basically just says that this cookie is valid for this domain and any subdomains. The following non-doctored image proves it:
Set two cookies, one with a specified domain and one without.
This results in the following two cookies
You will notice that the domains are a bit different. One has a ‘.’ while the other does not.
So. All this to say, if you are developing locally (or on a site without a TLD) then you will need to save your cookies without a “domain”. This will ensure that the cookie is accessible. Otherwise you will probably need to create yourself some sort of domain with a TLD.