如何向所有子域 URL 添加 cookie?
How to add a cookie to all subdomain URLs?
有一个地址像x.y.z.com。
我想在包含 .y.z.com 的所有 URL 中设置名为 BREEZESESSION 的 cookie。
在他们的 URL.
结束时
(例如,我想在 x.y.z.com 或 j.y.z.com 或 u.y.z.com ,任何以 .y.z.com 结尾的域 )
我试过这段代码,但它不起作用:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
var cc = new CookieContainer();
cc.Add(new Cookie("BREEZESESSION", "SomeValueHere", "/", "*.y.z.com"));
request.CookieContainer = cc;
var response = await request.GetResponseAsync();
var x = new StreamReader(response.GetResponseStream()).ReadToEnd();
从域中删除 *
:
new Cookie("BREEZESESSION", "SomeValueHere", "/", "y.z.com")
MDN HTTP Cookies documentation 声明设置域包括子域。
The Domain attribute specifies which hosts can receive a cookie. If unspecified, the attribute defaults to the same host that set the cookie, excluding subdomains. If Domain is specified, then subdomains are always included. Therefore, specifying Domain is less restrictive than omitting it. However, it can be helpful when subdomains need to share information about a user.
For example, if you set Domain=mozilla.org, cookies are available on subdomains like developer.mozilla.org.
有一个地址像x.y.z.com。
我想在包含 .y.z.com 的所有 URL 中设置名为 BREEZESESSION 的 cookie。 在他们的 URL.
结束时(例如,我想在 x.y.z.com 或 j.y.z.com 或 u.y.z.com ,任何以 .y.z.com 结尾的域 )
我试过这段代码,但它不起作用:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
var cc = new CookieContainer();
cc.Add(new Cookie("BREEZESESSION", "SomeValueHere", "/", "*.y.z.com"));
request.CookieContainer = cc;
var response = await request.GetResponseAsync();
var x = new StreamReader(response.GetResponseStream()).ReadToEnd();
从域中删除 *
:
new Cookie("BREEZESESSION", "SomeValueHere", "/", "y.z.com")
MDN HTTP Cookies documentation 声明设置域包括子域。
The Domain attribute specifies which hosts can receive a cookie. If unspecified, the attribute defaults to the same host that set the cookie, excluding subdomains. If Domain is specified, then subdomains are always included. Therefore, specifying Domain is less restrictive than omitting it. However, it can be helpful when subdomains need to share information about a user.
For example, if you set Domain=mozilla.org, cookies are available on subdomains like developer.mozilla.org.