check for cookies

M

mike parr

I'm trying to do a check to see if the client browser has cookies
enabled. But my code below always gives me the value for acceptsCookies
= true, whether the machine has cookies enabled or not.

Can anybody help me out with this?


private void Page_Load(object sender, System.EventArgs e)
{
if (!(Page.IsPostBack))
{
//check browser accepts cookies
HttpCookie checkCookies = new HttpCookie("checkCookies");
checkCookies.Values["userName"] = "mike";
Response.Cookies.Add(checkCookies);

bool acceptsCookies = false;

if (Request.Cookies["checkCookies"].Values["userName"] == null)
{
acceptsCookies = false;
}
else
{
acceptsCookies = true;

//Delete test cookie
Response.Cookies["checkCookies"].Expires =
DateTime.Now.AddDays(-1);
}

lblInfo.Text = acceptsCookies ? "Accepts cookies" : "Doesn't accept
cookies";
}
}



Cheers,

Mike
 
B

bruce barker

the only way to know if the broswe is accepting cookies, is to set a cookie,
send it to the browser, and have the browser post it back. on postback,
check to see if your cookie value is there, this is usually done with a
sniffer page for the site.

-- bruce (sqlwork.com)
 
G

Guest

I think you need to set the cookie on one page and check it on another page. Otherwise you may get inaccurate results, I don't think its a problem with your code. I remember I had the same issue with classic ASP ages ago and had to do the same thing to resolve it. So just split your code into two pages as shown below and it will work

[Page1.aspx

HttpCookie checkCookies = new HttpCookie("checkCookies")
checkCookies.Values["userName"] = "mike"
Response.Cookies.Add(checkCookies)
Response.Cookies["checkCookies"].Expires =DateTime.Now.AddDays(1)
Response.Redirect("Page2.aspx")

[Page2.apx]
bool acceptsCookies = false

//Notice how I have changed Request.Cookies["checkCookies"].Values["userName"] to Request.Cookies["checkCookies"] below
// Or you *may* get a System.NullReferenceException. I say *may* becuase i got it only in Mozilla 1.4 and not in IE6.0, weird
// With IE6.0 whether I blocked all cookies or not it was resolving to true, so I am not sure what is happening ther

if (Request.Cookies["checkCookies"] == null

acceptsCookies = false

els

acceptsCookies = true

//Delete test cooki
Response.Cookies["checkCookies"].Expires = DateTime.Now.AddDays(-1)


lblInfo.Text = acceptsCookies ? "Accepts cookies" : "Doesn't accept cookies"


References
http://msdn.microsoft.com/library/d...us/dv_vstechart/html/vbtchaspnetcookies101.as

Rasik
 
M

mike parr

Rasika,

I've tried what you suggested and it still returns true whether or not
cookies are enabled or disabled or my machine. I've also tried the code
in the Microsoft link, and I get the same result :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vste
chart/html/vbtchaspnetcookies101.asp

Yet my old ASP code works fine at detecting whether cookies are enabled
or not. This is driving me crazy! Surely someone else has encountered
this problem before?


Mike
 
D

Doug

When you say "whether or not cookies are enabled or disabled or my
machine" are you talking about disabling them in the browser? In IE?
In the Internet or Intranet zone? Because the Privacy slider used to
disable cookies in IE is only for the internet, not Intranet, which is
probably where your dev machine is located.

To disable cookies in Intranet zone, put http://localhost or
http://<machine name> into the Security Restricted Zones list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Cookies C# 3
Way to check for cookies 4
Browser cookies enabled testing 0
Browser cookies are enabled - test 0
Cookies 1
problem with cookies and graphics 0
Cookies during error handling 1
cookies.... 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top