Question

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

Was it helpful?

Solution

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.

OTHER TIPS

@Mattew is right the only way to find out is to set a cookie, redirect, then check it. Here is a C# function to preform that check you can put this in your page load event:

private bool cookiesAreEnabled()
{
bool cookieEnabled = false;

if(Request.Browser.Cookies)
{
   //Your Browser supports cookies 
   if (Request.QueryString["TestingCookie"] == null)
   {
     //not testing the cookie so create it
     HttpCookie cookie = new HttpCookie("CookieTest","");
     Response.Cookies.Add(cookie);

     //redirect to same page because the cookie will be written to the client computer, 
     //only upon sending the response back from the server 
     Response.Redirect("Default.aspx?TestingCookie=1")
   }
   else
   {
     //let's check if Cookies are enabled
      if(Request.Cookies["CookieTest"] == null)
      {
        //Cookies are disabled
      }
      else
      {
        //Cookies are enabled
        cookieEnabled = true;
      }   
   }

}
else
{
  // Your Browser does not support cookies
}
return cookieEnabled;
}


You can do it in javascript also, this way :

function cookiesAreEnabled()
{   
    var cookieEnabled = (navigator.cookieEnabled) ? 1 : 0;  

    if (typeof navigator.cookieEnabled == "undefined" && cookieEnabled == 0){   
    document.cookie="testcookie";   
    cookieEnabled = (document.cookie.indexOf("test­cookie") != -1) ? 1 : 0; 
    }   

  return cookieEnabled == 1;
}

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

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

meda's c# function works though you have to change the line:

HttpCookie cookie = new HttpCookie("","");

to

HttpCookie cookie = new HttpCookie("CookieTest","CookieTest");

Essentially the same solution as meda, but in VB.NET:

Private Function IsCookieDisabled() As Boolean
    Dim currentUrl As String = Request.RawUrl
    If Request.Browser.Cookies Then
        'Your Browser supports cookies 
        If Request.QueryString("cc") Is Nothing Then
            'not testing the cookie so create it
            Dim c As HttpCookie = New HttpCookie("SupportCookies", "true")
            Response.Cookies.Add(c)
            If currentUrl.IndexOf("?") > 0 Then
                currentUrl = currentUrl + "&cc=true"
            Else
                currentUrl = currentUrl + "?cc=true"
            End If
            Response.Redirect(currentUrl)
        Else
            'let's check if Cookies are enabled
            If Request.Cookies("SupportCookies") Is Nothing Then
                'Cookies are disabled
                Return True
            Else
                'Cookies are enabled
                Return False
            End If
        End If
    Else
        Return True
    End If
End Function

You can also check the value of Request.Browser.Cookies. If true, the browser supports cookies.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top