简体   繁体   中英

Cross Domain Cookies Problem (ASP.NET)

i have a problem with cross-domain cookies. I read a lot of documentation about sharing cookies between subdomains. The main idea of all articles is set Domain property to something like ".mydomain.com". I've created two domains on local IIS server - test1.local.boo and test2.local.boo . They works great and visible with browser. I have the following code:

Site test1 - Writes cookie:

HttpCookie myCookie = new HttpCookie("TestCookie");
myCookie.Domain = ".local.boo";
myCookie["msg"] = "Welcome from Cookie";
Response.Cookies.Add(myCookie);

Site test2 - Reads cookie:

HttpCookie cookie = Request.Cookies["TestCookie"];
if (cookie != null)
{
    Response.Write(cookie["msg"]);
}
else
{
    Response.Write("FAILED");
}

This code always shows FAILED message. So it means that second site can't read cookie from the same subdomain. Where is my mistake??

You can check if the cookie headers are being returned by the browser by using a web debugger such as fiddler .

It will show you the headers and cookies sent for every request and response, so you can see if the correct domain has been set and what happens with the request to the second domain.

Hmm...The problem was in browser...Opera browser doesn't send cookie to other site on the same subdomain. Firefox and IE works great. Anyway, thank you guys!

Some notes: if you want remove such cookie from other subdomain then you need to set a Domain property to something like: .mydomain.com - i've spend a lot of time by trying to figure it out. Hope it helps for somebody

In IIS 7

Add this to your web.config

<system.webserver>
    <httpProtocol>
        <customHeaders>
            <add name="p3p" value="CP=&quot;NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM&quot;" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

In II6.

  • Run inetmgr
  • Expand [Server] > Web Sites
  • Right clic in [Your site]
  • Properties
  • HTTP Headers
  • Add...
  • Custom header name: p3p
  • Custom header value: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"
  • OK
  • OK

尝试设置expires属性,或者在usersession结束时删除它的可能性。

尝试将过期日期设置为将来某个时间点:

cookie.Expires = DateTime.Now.AddYears(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM