views:

1448

answers:

5

What is the best method for determining if a users browser has cookies enabled in ASP.NET

+1  A: 

Write a cookie, redirect, see if you can read the cookie.

JacquesB
+12  A: 

Set a cookie, force a redirect to some checking page and check the cookie.

Or set a cookie on every pageload, if it's not already set. For instance, I assume this is to check if cookies are supported to display a message when they try to login that they need to enable cookies. Set your login cookie to some default value for guest users if they don't have the cookie set yet. Then on your login page, check for the user cookie, and if it's not set, then display your message.

Matthew Scharley
+1  A: 

Well I think if we can save cookie in Global.ASAX session start and read that on page.. isnt that best way?

A: 

I have expericenced strage secnario

I have disabled cookies

I tried creating a cookie and reading it in Global.aspx which returns true.

but actually the cookie is not created.

any help on how to resolve this.

nuwan
A: 

this is the best way

taken from http://www.eggheadcafe.com/community/aspnet/7/42769/cookies-enabled-or-not-.aspx

function cc()
{
 /* check for a cookie */
  if (document.cookie == "") 
  {
    /* if a cookie is not found - alert user -
     change cookieexists field value to false */
    alert("COOKIES need to be enabled!");

    /* If the user has Cookies disabled an alert will let him know 
        that cookies need to be enabled to log on.*/ 

    document.Form1.cookieexists.value ="false"  
  } else {
   /* this sets the value to true and nothing else will happen,
       the user will be able to log on*/
    document.Form1.cookieexists.value ="true"
  }
}

thanks to Venkat K

nuwan