I have two questions. I understand that if I specify the domain as .mydomain.com
(with the leading dot) in the cookie that all subdomains can share a cookie.
Please everyone note that you can set a cookie from a subdomain on a domain.
(sent in the response for requesting subdomain.mydomain.com
)
Set-Cookie: name=value; Domain=mydomain.com // GOOD
But you CAN'T set a cookie from a domain on a subdomain.
(sent in the response for requesting mydomain.com
)
Set-Cookie: name=value; Domain=subdomain.mydomain.com // Browser rejects cookie
According to the specifications RFC 6265 section 5.3.6 Storage Model
If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps.
and RFC 6265 section 5.1.3 Domain Matching
Domain Matching
A string domain-matches a given domain string if at least one of the following conditions hold:
The domain string and the string are identical. (Note that both the domain string and the string will have been canonicalized to lower case at this point.)
All of the following conditions hold:
The domain string is a suffix of the string.
The last character of the string that is not included in the domain string is a %x2E (".") character.
The string is a host name (i.e., not an IP address).
So "subdomain.mydomain.com" domain-matches "mydomain.com", but "mydomain.com" does NOT domain-match "subdomain.mydomain.com"
Check this answer also.