469,934 Members | 1,883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

C# Client browser, cookies enabled?

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
{
}
}

Apr 28 '06 #1
6 7619
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?

Apr 28 '06 #2
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.
dawson wrote:
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?


Apr 28 '06 #3
> 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
Apr 28 '06 #4
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.

Hans Kesting wrote:
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


Apr 28 '06 #5
Anyone please?

dawson wrote:
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.

Hans Kesting wrote:
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


May 2 '06 #6
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
May 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sri. | last post: by
reply views Thread by Sri | last post: by
1 post views Thread by Peter Row | last post: by
reply views Thread by dawson | last post: by
3 posts views Thread by =?Utf-8?B?Qkw=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.