What is the best method for determining if a users browser has cookies enabled in ASP.NET
By default, ASP.NET uses a non-persistent cookie to store the session state. However, if a user has disabled cookies on the browser, session state information cannot be stored in a cookie.
Set a cookie, force a redirect to some checking page and check the cookie.
Or set a cookie on every pageload, if it's not already set. For instance, I assume this is to check if cookies are supported to display a message when they try to login that they need to enable cookies. Set your login cookie to some default value for guest users if they don't have the cookie set yet. Then on your login page, check for the user cookie, and if it's not set, then display your message.
@Mattew is right the only way to find out is to set a cookie, redirect, then check it. Here is a C# function to preform that check you can put this in your page load event:
private bool cookiesAreEnabled() { bool cookieEnabled = false; if(Request.Browser.Cookies) { //Your Browser supports cookies if (Request.QueryString["TestingCookie"] == null) { //not testing the cookie so create it HttpCookie cookie = new HttpCookie("CookieTest",""); Response.Cookies.Add(cookie); //redirect to same page because the cookie will be written to the client computer, //only upon sending the response back from the server Response.Redirect("Default.aspx?TestingCookie=1") } else { //let's check if Cookies are enabled if(Request.Cookies["CookieTest"] == null) { //Cookies are disabled } else { //Cookies are enabled cookieEnabled = true; } } } else { // Your Browser does not support cookies } return cookieEnabled; }
function cookiesAreEnabled() { var cookieEnabled = (navigator.cookieEnabled) ? 1 : 0; if (typeof navigator.cookieEnabled == "undefined" && cookieEnabled == 0){ document.cookie="testcookie"; cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? 1 : 0; } return cookieEnabled == 1; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With