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
 
A

Ahmet Mithat Bostanci

Hi,
you should try setting cookies in page1.aspx, redirecting to page2.aspx and
checking cookies in page2.aspx...

Hope that helps,
AMB
 

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


Top