Check if browser accepts cookies

16,125

Solution 1

According to MSDN there is no way to determine cookies is allowed or disabled by user. The only way to find it out is through writing it and then reading it.

Check the section "Determining Whether a Browser Accepts Cookies" here.

It also has examples which show how to read and write cookies, and states:

The Cookies property does not indicate whether cookies are enabled. It indicates only whether the current browser inherently supports cookies.

Solution 2

I don't know any way to do it in asp.NET. All you can do is to create a cookie and try to read it in the postback.
You can see example of how to do it (this is in vb.NET but I'm sure you can find in google many other examples and in C#): http://forums.asp.net/t/1044823.aspx

Solution 3

As everyone stated, you essentially need to set, redirect, detect (unless you want only client side detection in that case javascript can do the check) There's a control for this someone wrote.

Solution 4

I'm don't know other method: set the cookie,check if the cookie exists.

Share:
16,125
Dietpixel
Author by

Dietpixel

Updated on June 10, 2022

Comments

  • Dietpixel
    Dietpixel almost 2 years

    Is there a way that I can check if the browser will allow cookies.

    Request.Browser.Cookies 
    

    Doesn't work when cookies are disabled. It says the browser can support them.

    The only other thing I can think of is to try to set a test cookie and then check to see if it ever got set.

    Is there anyway to make sure cookies are actually enabled.

    • Shekhar_Pro
      Shekhar_Pro almost 13 years
      One way to determine whether cookies are accepted is by trying to write a cookie and then trying to read it back again. If you cannot read the cookie you wrote, you assume that cookies are turned off in the browser.
    • Aaron Anodide
      Aaron Anodide almost 13 years
      It's not surprising that the only answer is to write the cookie out and try to read it back when you think bout how cookies work - an http response set one with a header and then the browser only spits that back when it requests from that exac same domain name - if three was another way to do it, then cookies wouldn't be good for things like authentication and such
  • Dietpixel
    Dietpixel almost 13 years
    So far that appears like the only option.
  • Naor
    Naor almost 13 years
    @Dietpixel: I faced this issue before and after many chackings I realized this is my best and simple option (if not the only one).
  • Max Strater
    Max Strater about 9 years
    From the documentation: "The Cookies property indicates whether the browser application supports cookies. If the user has disabled cookies in their application, the Cookies property will not be affected."