views:

284

answers:

3

I want to check if a user has cookies enabled. Most solutions involve: 1. creating a cookie 2. redirect the user to a custom page or the same page. 3. Read the cookie.

The issue I have is in the 2nd step. Should I use a query string while doing a response.redirect so that in the next trip I know the cookie has already been set and that I should try to read it? What if the user hard codes the URL(along with that query string) in the browser, while accessing the website? Also, if I find that the cookies are enabled and I set a session variable to say that cookies are enabled on this browser, so dont check again in that session. Is that OK? If session is available, is that a good enough indicator that cookies are enabled?

I want to minimize these double trips to each page for checking cookies.

A: 

Never pass a querystring. You already hinted at it above, but what if some trickster figures our the url and decides they want to pass their own querystring?

If the user has cookies set up, you can set the session and check that. Always check the session.

CitizenBane
Thanks CitizenBane..Ok..If cookies are enabled, I can set the session and check for it in all subsequent trips to the website.If cookies are disabled, would there be any way to prove that?
AgentHunt
You wouldn't get a session. Asp.net sessions are dependent on you having cookies enabled. Try it out for yourself - disable the cookies and debug through your code.
CitizenBane
A: 

Hi,

instead of using this technique which involves multiple steps and pages, and extra waiting time for the enduser, can't you just use the HttpBrowserCapabilities class? This particular class has a Cookies property:

HttpBrowserCapabilities.Cookies Property

Grz, Kris.

XIII
I believe this property checks to see if the browser supports cookies.I want to check if the user has the cookies enabled / disabled.I think both are different things. Help me if I am wrong here.
AgentHunt
+1  A: 

I would use javascript to make an asynchronous request and check to see if the cookies that were set were handed back in this request.

dlamblin
I need to place this code just in case javascript is disabled :(
AgentHunt