locked
Cookie Enabled/Disabled problem in IE RRS feed

  • Question

  • User861911889 posted

    Please please I need your help

    I want to check whether cookie is enabled in user's computer. I have written code in jquery. My code is working fine in Chrome.But failed to work for IE11. It is showing cookie value though cookie is blocked. 

     $(function () {
            $.cookie("isCookieEnabled", "Yes", { expires: 30 });
            var cookieVal = $.cookie("isCookieEnabled");
            if (cookieVal == "Yes") {
                alert("Enabled " + cookieVal);
            }
            else {
                alert("Disabled");
            }
    })

    Wednesday, December 16, 2015 10:20 AM

All replies

  • User-369506445 posted

    hi

    please try below code :

     <input type="button" value="checked" onclick="areCookiesEnabled()"/>
             <script>
                 function areCookiesEnabled() {
                     var cookieEnabled = navigator.cookieEnabled;
    
                     // When cookieEnabled flag is present and false then cookies are disabled.
                     if (cookieEnabled === false) {
                         alert("false");
                     }
    
                     // try to set a test cookie if we can't see any cookies and we're using 
                     // either a browser that doesn't support navigator.cookieEnabled
                     // or IE (which always returns true for navigator.cookieEnabled)
                     if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
                         document.cookie = "testcookie=1";
    
                         if (!document.cookie) {
                             alert("false");
                         } else {
                             alert("true");
                         }
                     }
    
                     alert("true");
                 }
        </script>

    Wednesday, December 16, 2015 10:35 AM
  • User861911889 posted

    Hi Vahid thank you for your msg. Is this the following line correct

    if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {

    I am going to try it now...with

    if (!document.cookie && (cookieEnabled === null || false)) {

    same result - working for chrome not IE11
    Wednesday, December 16, 2015 10:57 AM
  • User-369506445 posted

    i try again it, and it work in my ie 11

    but if you want be sure about it , you can check it in code behind

    you can add Page_Init event to your page below like :

    protected void Page_Init(object sender, EventArgs e)
            {
                var IsEnable = cookiesAreEnabled();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
              
            }
    
            private bool cookiesAreEnabled()
            {
                bool cookieEnabled = false;
    
                if (Request.Browser.Cookies)
                {
                   
                    if (Request.QueryString["TestingCookie"] == null)
                    {
                      
                        HttpCookie cookie = new HttpCookie("CookieTest", "");
                        Response.Cookies.Add(cookie);
                        Response.Redirect("Default.aspx?TestingCookie=1"); // you must put your current page name here,in my project this page is default.aspx
                    }
                    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;
            }

    Wednesday, December 16, 2015 12:15 PM
  • User861911889 posted

    I am using MVC I have written similar kind of code and that too working for chrome not IE... I have blocked cookies in IE by

    Internet Options - Privacy - Advanced - First Party Cookies - Blocked , Third Party Cookies - Blocked

    and my jquery code

    <div class="row">
    <input type="button" value="checked" onclick="areCookiesEnabled()"/>
             <script>
                 function areCookiesEnabled() {
                     var cookieEnabled = navigator.cookieEnabled;                 
                     if (cookieEnabled === false) {
                         alert("false");
                     }                
                     if (!document.cookie && (cookieEnabled === null || false)) {
                         document.cookie = "testcookie=1";
    
                         if (!document.cookie) {
                             alert("false");
                         } else {
                             alert("true");
                         }
                     }                 
                 }
        </script>
    </div>

    It is showing cookieEnabled = true and document.cookie = "isCookieEnabled=Yes"

    Wednesday, December 16, 2015 12:44 PM
  • User-369506445 posted

    if you use mvc , you can create a attribute and put on your action or controller

    1-first create a New folder called attribute

    2-next create a class called EnsureCookiesAttribute

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Web;
    using System.Web.Mvc;
    
    namespace WebApplication2.Attribute
    {
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
        public class EnsureCookiesAttribute : FilterAttribute, IAuthorizationFilter
        {
            private readonly string _cookieName;
            private readonly bool _specificCookie;
    
            /// <summary>
            /// The name of the cookie to use to ensure cookies are enabled.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible",
                Justification = "Field is public so that the default value may be modified.")]
            public static string DefaultCookieName = "SupportsCookies";
    
            public const string CookieCheck = "cookieCheck";
    
            /// <summary>
            /// Checks to make sure cookies are generally enabled.
            /// </summary>
            public EnsureCookiesAttribute() : this(null) 
            {
            }
    
            /// <summary>
            /// Checks to make sure a cookie with the given name exists
            /// </summary>
            /// <param name="cookieName">The name of the cookie</param>
            public EnsureCookiesAttribute(string cookieName)
            {
                if (String.IsNullOrEmpty(cookieName))
                {
                    cookieName = DefaultCookieName;
                }
                else
                {
                    _specificCookie = true;
    
                }
    
                QueryString = CookieCheck;
    
                _cookieName = cookieName;
            }
    
            /// <summary>
            /// The name of the cookie to check for.
            /// </summary>
            public string CookieName
            {
                get { return _cookieName; }
            }
    
            /// <summary>
            /// The querystring parameter to use to see if a test cookie has been set.
            /// </summary>
            public string QueryString { get; set; }
    
            protected static CookiesNotEnabledException CreateBrowserException()
            {
                return new CookiesNotEnabledException("Your browser does not support cookies.");
            }
    
            protected static CookiesNotEnabledException CreateNotEnabledException()
            {
                return new CookiesNotEnabledException("You do not have cookies enabled.");
            }
    
            #region Implementation of IAuthorizationFilter
    
            /// <summary>
            /// Called when authorization is required.
            /// </summary>
            /// <param name="filterContext">The filter context.</param>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes"
                , Justification = "Should swallow exceptions if a cookie can't be set.  This is the purpose of the filter.")]
            public void OnAuthorization(AuthorizationContext filterContext)
            {
                if (filterContext == null)
                    throw new ArgumentNullException("filterContext");
    
                var request = filterContext.HttpContext.Request;
                var response = filterContext.HttpContext.Response;
    
                if (!request.Browser.Cookies)
                    throw CreateBrowserException();
    
                string currentUrl = request.RawUrl;
    
                var noCookie = (request.Cookies[CookieName] == null);
                if (!_specificCookie && noCookie && request.QueryString[QueryString] == null)
                {
                    try
                    {
                        // make it expire a long time from now, that way there's no need for redirects in the future if it already exists
                        var c = new HttpCookie(CookieName, "true") { Expires = DateTime.Today.AddYears(50) };
                        response.Cookies.Add(c);
    
                        currentUrl = currentUrl + (currentUrl.Contains('?') ? "&" : "?") + QueryString + "=true";
    
                        filterContext.Result = new RedirectResult(currentUrl);
                        return;
                    }
                    catch
                    {
                    }
                }
    
                if (noCookie)
                    throw CreateNotEnabledException();
            }
    
            #endregion
        }
    
        /// <summary>
        /// Thrown when cookies are not supported.
        /// </summary>
        [Serializable]
        public class CookiesNotEnabledException : HttpException
        {
            public CookiesNotEnabledException()
            {
            }
    
            protected CookiesNotEnabledException(SerializationInfo info, StreamingContext context)
                : base(info, context)
            {
            }
    
            public CookiesNotEnabledException(string message)
                : base(message)
            {
            }
    
            public CookiesNotEnabledException(string message, Exception innerException)
                : base(message, innerException)
            {
            }
        }
    }

    3- in your controller call that

    using WebApplication2.Attribute; //add your namespace Attribute Class 

    [EnsureCookiesAttribute] [HandleError(ExceptionType = typeof(CookiesNotEnabledException), View = "NoCookies")] public ActionResult Index() { return View(); }

    i hope it can be helpful

    Wednesday, December 16, 2015 12:59 PM
  • User861911889 posted

    Thank you so much. You are doing so much for me. But same result working for chrome but in IE var noCookie = (request.Cookies[CookieName] == null); is coming false. I am now thinking am I doing the right thing in disabling cookie in IE. 

    Setting - Internet options - Advanced - block.

    Are there any other settings that I am missing.....

    Wednesday, December 16, 2015 1:29 PM
  • User-369506445 posted

    if you use the last code in your mvc project , it's correct and it checked it surly

    now if you want be sure the cookie is disable , you can create a sample project with authentication form and login with a test user , because it store in cookie and in your controller

    check that Does hold the user information?

     

    Wednesday, December 16, 2015 1:45 PM
  • User861911889 posted

    Ya atlast your last code is working if and only if I delete previously set cookies from IE.... is there a way where I can delete old cookies prior to check whether the site supports cookie or not.... pls help I think you can do that... thank youuuu for making my code work.

    Wednesday, December 16, 2015 2:14 PM
  • User861911889 posted

    Just saw another issue in your code Vahid, if you delete all cookies in IE and try to see if the browser supports cookie it will display the exception page regardless of cookie being enabled or disabled.

    Wednesday, December 16, 2015 4:37 PM
  • User861911889 posted

    Hi Vahid,

    Are you sure this code runs for IE11...

     <input type="button" value="checked" onclick="areCookiesEnabled()"/>
             <script>
                 function areCookiesEnabled() {
                     var cookieEnabled = navigator.cookieEnabled;
    
                     // When cookieEnabled flag is present and false then cookies are disabled.
                     if (cookieEnabled === false) {
                         alert("false");
                     }
    
                     // try to set a test cookie if we can't see any cookies and we're using 
                     // either a browser that doesn't support navigator.cookieEnabled
                     // or IE (which always returns true for navigator.cookieEnabled)
                     if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
                         document.cookie = "testcookie=1";
    
                         if (!document.cookie) {
                             alert("false");
                         } else {
                             alert("true");
                         }
                     }
    
                     alert("true");
                 }
        </script>
    can you pls clarify the line
    if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false)) {
    what is
    /*@cc_on!@*/ pls
     
    Thursday, December 17, 2015 2:00 PM