Checking if Browser Supports Cookies (ASP.NET)

Most of the time, when you check forums about checking if a user's browser has cookies enabled, you will see posts that say you should use the following:

// check if user's browser supports cookies
if (!Request.Browser.Cookies)
{
    // sorry, can't support it so show message to user
}

But this code only checks to see if a user's browser supports cookies. Generally, all major browsers today support cookies so this check is kind of pointless and still does not tell you if the user's browser has cookies enabled.



To check if cookies are enabled (meaning the user's browser settings are set to accept/allow cookies), unfortunately, there is no simple property like Browser.Cookies that you can check. Instead, for many sites like msn or hotmail, that require cookies, they do the check similar to the following process:

1) When user comes to their starting page, they set a cookie that they can check.

2) Then the user is redirected automatically to a page that checks for this cookie that was set.

3) If the cookie is set, then the user's browser supports cookie and cookies are enabled. Otherwise, the user's browser does not support cookies or cookies are turned off for this user so you can display a message to notify the user.

So to make this super simple, I've created a Custom Web Control that allows you to just drag and drop it onto something like your Login page that checks for cookies and if cookies are disabled, notifies the user.

The way this is implemented is the control is added to your page. Then the control creates a hidden IFRAME on the page that redirects back to the same page it is being hosted on with a simple querystring parameter. So the page first sets the cookie, and then the iFrame navigates to this page to check if the cookie was set properly and a message is displayed if the cookie is not found on the redirected hidden iFrame browser.

The nice thing about this approach is that you don't need to write any code to get this working and you don't need to add an extra page in your site just to check for a cookie and do redirects. This does everything for you.

You can download the code here:

CheckCookiesEnabledControl.cs

To use this on your page, put this code file in your project and build. Then drag and drop it from your toolbox or manually enter it like this:

<cc1:CheckCookiesEnabledControl ID="CheckCookiesEnabledControl1" runat="server" />

I get this error

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 139: sbJavascript.AppendLine("

");
Line 140: m_litJavascript.Text = sbJavascript.ToString();
Line 141: Page.Header.Controls.Add(m_litJavascript);
Line 142:
Line 143: // add the iframe to check the cookie

How to implement this?

I added the.cs page in the project and tried as said but i am unable to, get this worked.Can u send me the Sample working project.

Good Idea

I'll try this control bru great idea, hope it works... by the way nice pic, guess she's yoou girl, but don't be mean, we know she's your nookie but you shouldn't expose her...

-Cheers!

tanx tanx tanx a lot

tanx tanx tanx a lot