C# Client browser, cookies enabled?

  • Thread starter Thread starter dawson
  • Start date Start date
D

dawson

I started off by trying to use the HttpCapabilitiesBase.Cookies
Property (Note: This property is new in the .NET Framework version 2.0)
however it kept on returning true even when I disabled cookies in both
FireFox and Internet Explorer.

After a bit of googling I found that a lot of people were creating
cookies, then re-reading them; obviously if they could re-read the
cookie then they knew that the client's browser accepts cookies!
Simple. However, after writing a similar class to the ones I viewed
online, I'm still getting true returned, even when I disable cookies in
both FireFox and Internet Explorer.

With FireFox's cookie dialog open, I ran the application with cookies
turned off and indeed, FireFox showed no cookies were stored during the
HTTP request, however my code once again returned true!

When running the application with cookies turned on, FireFox showed one
cookie stored during HTTP request, and the code returned true.

Please advise.

public bool HttpBrowserCookieCheck(HttpRequest Request,
HttpResponse Response)
{
try
{
// Create cookie object
HttpCookie cookieCheck = null;

// Request cookie from client's browser
cookieCheck =
Request.Cookies["HttpBrowserCookieCheck"];

// Check whether the cookie exists on client's
browser
if (cookieCheck == null) // Client browser does not
support cookies !! noooo !!
{
return false;
}
else // Client browser supports cookies !! yay !!
{
// Remove test cookie

Response.Cookies.Remove("HttpBrowserCookieCheck");
return true;
}
}
catch
{
return false;
}
finally
{
}
}

public void HttpBrowserCookieCreate(HttpRequest Request,
HttpResponse Response)
{
try
{
// Create cookie object
HttpCookie cookieCreate = new
HttpCookie("HttpBrowserCookieCheck");

// Set the cookies value
cookieCreate.Value = "true";

// Add the cookie
Response.Cookies.Add(cookieCreate);
}
catch
{
}
finally
{
}
}
 
I have downloaded HTTP Analyzer to see if I can get a better
understanding of what's going on. However if anything it's confused me
more.

If I disable cookies in FireFox and run the web application, FireFox's
cookie dialogue tells me no cookies were recieved/stored, which is
correct. However HTTP Analyzer shows me the cookie that I sent in the
script above, so ... still a bit lost.

Basically, I need to get my code to request a cookie from the client's
browser, if it doesn't exist; return false. Which I believe the above
code should do?
 
Right, the method below checks whether or not the specified cookie
exists. The problem is, is that the cookie DOES NOT exist, I'm looking
at the cookies window in FireFox and it's empty, but this code is
continually returning true..

// retrieve cookie value
public bool GetCookie(HttpRequest Request)
{
HttpCookie MyCookie = Request.Cookies["MyCookie"];

// check cookie exists
if(MyCookie != null)
{
return true;
}
else
{
return false;
}
}

Please advise.
 
I started off by trying to use the HttpCapabilitiesBase.Cookies
Property (Note: This property is new in the .NET Framework version 2.0)
however it kept on returning true even when I disabled cookies in both
FireFox and Internet Explorer.

The Capabilities class does not test the current browser, rather it
reads the browser identification string to make a guess as to what
browser is making the request. From that guess it will report that
"this" browser is *capable* of using cookies etc, not whether it is
actually enabled.


Hans Kesting
 
Thank you, fortunatly I stopped using it right at the beginning of the
thread.

Could you read my other posts and see if you can see what the problem
is?

Thanks.
 
Anyone please?
Thank you, fortunatly I stopped using it right at the beginning of the
thread.

Could you read my other posts and see if you can see what the problem
is?

Thanks.
 
After reading your original post again, I noticed this part:

HttpCookie cookieCreate = new HttpCookie("HttpBrowserCookieCheck");

// Set the cookies value
cookieCreate.Value = "true";

// Add the cookie
Response.Cookies.Add(cookieCreate);

With this code you will create a "session" cookie, one that will not
be persisted when the browser closes. I'm not sure what happens to
these cookies when you tell FireFox to "ignore cookies". It is
possible that the "ignore" only works on persisted cookies.
Try adding an expiry-date (doesn't have to be later than "tomorrow")
and see if that helps.


Hans Kesting
 
Back
Top