how to check cookie enabled for the browser or not?

B

BL

if (Request.Browser.Cookies)
{
// Cookies supported
}
else
{
// Web browser not supports cookies
}


Code above will tell you does web browser supports cookie technology, but a
visitor could disable cookies in web browser's privacy settings. In that
case, Request.Browser.Cookies will still return true but your cookies will
not be saved.

what is the best way in asp.net 2.0 to check cookies disable or enabled in
web browser's privacy settings ?

Thank you.
BL
 
G

GridView Newbie

I could not say if this is the "best" way but one way I see often is to write
a test cookie on page load and then post back to the same url checking if you
can retrieve the cookie. The code below is from the forums on asp.net and
posted by Fredrik Normen. It illustrates in general how this strategy could
be implemented.

Hopefully you can use something like this to solve your problem. Otherwise,
you might have to use javascript to check if cookies are enabled and then
post that to a form/url to be stored in the session state.

Good Luck.

===================================================
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsCookieDisabled())
errorMsgLabel.Text = Resources.Resource.BrowserDontSupportCookies;

}


private bool IsCookieDisabled()
{
string currentUrl = Request.RawUrl;

if (Request.QueryString["cookieCheck"] == null)
{
try
{
HttpCookie c = new HttpCookie("SupportCookies", "true");
Response.Cookies.Add(c);

if (currentUrl.IndexOf("?") > 0)
currentUrl = currentUrl + "&cookieCheck=true";
else
currentUrl = currentUrl + "?cookieCheck=true";

Response.Redirect(currentUrl);
}
catch
{
}
}

if (!Request.Browser.Cookies || Request.Cookies["SupportCookies"] ==
null)
return true;

return false;
}

===================================================
 
B

bruce barker

this is usually done with a sniffer page. you set a value in the cookie and
redirect back to the same page and check to see if the cookie has a value.
usually you'd also check for javascript and any plugins you need at the same
time. you can get a commerical sniffer if you google.

-- bruce (sqlwork.com)
 
B

BL

Thank you for your quick reply

your code is deduct cookies status perfect in firefox and other browsers but
in internet explorer 6 and 7 this function always return false weather
cookies enabled or disabled

Thanks again

BL

GridView Newbie said:
I could not say if this is the "best" way but one way I see often is to write
a test cookie on page load and then post back to the same url checking if you
can retrieve the cookie. The code below is from the forums on asp.net and
posted by Fredrik Normen. It illustrates in general how this strategy could
be implemented.

Hopefully you can use something like this to solve your problem. Otherwise,
you might have to use javascript to check if cookies are enabled and then
post that to a form/url to be stored in the session state.

Good Luck.

===================================================
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsCookieDisabled())
errorMsgLabel.Text = Resources.Resource.BrowserDontSupportCookies;

}


private bool IsCookieDisabled()
{
string currentUrl = Request.RawUrl;

if (Request.QueryString["cookieCheck"] == null)
{
try
{
HttpCookie c = new HttpCookie("SupportCookies", "true");
Response.Cookies.Add(c);

if (currentUrl.IndexOf("?") > 0)
currentUrl = currentUrl + "&cookieCheck=true";
else
currentUrl = currentUrl + "?cookieCheck=true";

Response.Redirect(currentUrl);
}
catch
{
}
}

if (!Request.Browser.Cookies || Request.Cookies["SupportCookies"] ==
null)
return true;

return false;
}

===================================================

BL said:
if (Request.Browser.Cookies)
{
// Cookies supported
}
else
{
// Web browser not supports cookies
}


Code above will tell you does web browser supports cookie technology, but a
visitor could disable cookies in web browser's privacy settings. In that
case, Request.Browser.Cookies will still return true but your cookies will
not be saved.

what is the best way in asp.net 2.0 to check cookies disable or enabled in
web browser's privacy settings ?

Thank you.
BL
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top