PC Review


Reply
Thread Tools Rate Thread

how to check cookie enabled for the browser or not?

 
 
BL
Guest
Posts: n/a
 
      8th Sep 2008
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
 
Reply With Quote
 
 
 
 
GridView Newbie
Guest
Posts: n/a
 
      8th Sep 2008
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" wrote:

> 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

 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      8th Sep 2008
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)


"BL" wrote:

> 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

 
Reply With Quote
 
BL
Guest
Posts: n/a
 
      8th Sep 2008
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" wrote:

> 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" wrote:
>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check in server code if Javascript is enabled/disabled in browser GeezerButler Microsoft Dot NET Framework 0 19th Apr 2008 07:09 PM
Check browser cookie permission Ayaz Microsoft VB .NET 0 15th Mar 2006 01:01 PM
Cookie being Enabled? =?Utf-8?B?dGxjXzEzMjAwQGhvdG1haWwuY29t?= Windows XP Internet Explorer 7 17th Jul 2005 07:40 AM
Session cookie? Browser instance cookie? Ben Microsoft ASP .NET 3 3rd Jun 2004 04:41 AM
Transient-Cookie/Session-Cookie and multiple browser instances X Windows XP Internet Explorer 7 29th Apr 2004 11:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:48 PM.