Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some cookies have a '.' before the domain?

Trying to share cookies accross 2 domains in asp.net, for some reason 1 domain has a '.' before the domain, and the other doesn't.

Why is that?

e.g:

.staging.example.com

and

staging.example.com

Is this something to do with how I create the cookie, or a web.config change?

I am not using forms authentication, just creating a cookie manually.

Upd

I am setting the cookie domain like:

HttpCookie c = new HttpCookie("blah");
c.Value = "123";
c.Expires = DateTime.Now.AddHours(12);
c.Domain = ".staging.example.com";

Response.Cookies.Add(c);

For some reason not getting the '.' in the cookie.

What could be the issue?

like image 988
Blankman Avatar asked Oct 15 '22 08:10

Blankman


2 Answers

If you set a . before a domain name, e.g.

.staging.example.com

This means that any domain name that resides under that, will have access to that cookie. E.g. test01.staging.example.com would have access to whatever was in that cookie as if it had created it itself. Without the dot, its limited to the specific domain that is named.

like image 180
Amadiere Avatar answered Oct 19 '22 01:10

Amadiere


The cookie for .staging.example.com is also readable for each subdomain of that domain, e.g. www.staging.example.com, the other one is not.

like image 41
Residuum Avatar answered Oct 19 '22 02:10

Residuum