Sharing ASP.NET cookies across sub-domains

Set the property of Domain to .mydomain.example in each Cookies of two subdomains websites. Like:

Response.Cookies["test"].Value = "some value";
Response.Cookies["test"].Domain = ".mysite.example";

In Site A:

HttpCookie hc = new HttpCookie("strName", "value");
hc.Domain = ".mydomain.example"; // must start with "."
hc.Expires = DateTime.Now.AddMonths(3);
HttpContext.Current.Response.Cookies.Add(hc);

In Site B:

HttpContext.Current.Request.Cookies["strName"].Value

Leave a Comment