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:
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" />
Popular Articles
Last viewed:
- Clustered Index vs. Non-Clustered Index in SQL Server
- Fix for Visual Studio 2008 SP1 Design Mode Error - [text] could not be set on [property]
- Get Index Fragmentation Percentage for all Tables in SQL Server Database
- Silverlight Memory Leak DataGrid, DataForm, DataTemplate, etc...
- C# Free Component to Generate PDF - Convert HTML to PDF
- Change IIS ASP.NET Framework Version Programatically
Recent comments
- Aha! Problems with UpdatePanel? Sorted ...
1 day 1 hour ago - Some help required with this
1 day 6 hours ago - Never seen this issue
1 week 1 day ago - Error in query.ToList()
1 week 1 day ago - Thanks
1 week 2 days ago - Thanks
1 week 5 days ago - To get the data working,
2 weeks 1 day ago - If I manually change the
2 weeks 1 day ago - Handling EDM Relationship Metadata
2 weeks 1 day ago - About the itextsharp version
2 weeks 1 day ago
Similar
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Global.asax Events in IIS 6 and IIS 7 for Static Resources
- Create a Windows-style GroupBox in ASP.NET
- Changing the Application Pool Identity with Windows Server 2008 and IIS 7


tanx tanx tanx a lot
tanx tanx tanx a lot