Setting cookies without domains in iFrame

I have a website that has a cookie set, say www.domain.com, called aCookie. This cookie needs to be fetched from other subdomains, so I set the cookie domain to .domain.com.

I need to read and write a cookie from an iFrame that links to sub.domain.com. They are on the same domain, so there is no cross-site restriction or anything like that.

The weird thing is that if I write a cookie to www.domain.com and then try to change its value from sub.domain.com, a second cookie is created with the same name but with the domain set to sub.domain.com, even if I have explicitly set the domain to .domain.com.

After that, the weirdness happens because it won't be read or it will be set on one of the cookies but read from the other cookie.

Any idea why this is happening and how I can fix it, thanks?

Cookie.Set("directDepositMethod", directDepositMethod, { expires: 1, secure: true, path: '/', domain: ".domain.com" });

      

where set:

 if (typeof value != "undefined") { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = String.Empty;
                options.expires = -1;
            }
            var expires = String.Empty;
            if (options.expires && (typeof options.expires == "number" || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == "number") {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = "; expires=" + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            // CAUTION: Needed to parenthesize options.path and options.domain
            // in the following expressions, otherwise they evaluate to undefined
            // in the packed version for some reason...
            var path = options.path ? "; path=" + (options.path) : "";
            var domain = options.domain ? "; domain=" + (options.domain) : "";
            var secure = options.secure ? "; secure" : "";
            document.cookie = [name, "=", encodeURIComponent(value), expires, path, domain, secure].join("");
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != String.Empty) {
                var cookies = document.cookie.split(";");
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + "=")) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }

      

+3


source to share





All Articles