how to check cookie enabled for the browser or not?

  • Thread starter Thread starter BL
  • Start date Start date
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
 
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;
}

===================================================
 
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)
 
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
 
Back
Top