Scoping Cookies to a Domain
By default, cookies are associated with a specific domain. For example, if your site is
www.contoso.com, the cookies you write are sent to the server when users request any page from that site. (Except for cookies with a specific path value, as I explained in the section immediately preceding.) If your site has subdomains — for example, contoso.com, sales.contoso.com, and support.contoso.com — then you can associate cookies with a specific subdomain. To do so, set the cookie's Domain property, like this:
Code:
Response.Cookies("domain").Value = DateTime.Now.ToString
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "support.contoso.com"
When the domain is set in this way, the cookie will be available only to pages in the specified subdomain.
You can also use the Domain property to create a cookie that can be shared among multiple subdomains. For example, set the domain as follows:
Code:
Response.Cookies("domain").Value = DateTime.Now.ToString
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "contoso.com"
The cookie will then be available to the primary domain as well as to sales.contoso.com and support.contoso.com.