假设:
Domain 1: subdomain1.mydomain.com
Domain 2: subdomain2.mydomain.com
我使用下面的代码在“域1”上创建了一个cookie,并尝试访问“域2”上的cookie。
我的问题是“域2”不想识别cookie。是什么赋予了?我认为问题在于.Domain属性,但我把时间段放在前面,所以我错过了什么?
public void CreateCookie()
{
Boolean bNew = false;
HttpCookie oCookie = HttpContext.Current.Request.Cookies.Get("myData");
if (null == oCookie)
{
oCookie = new HttpCookie("myData");
bNew = true;
}
// Set the cookie value.
oCookie.Domain = ".mydomain.com";
oCookie.Secure = false;
oCookie["myid"] = "myid@whatever";
oCookie.Expires = DateTime.Now.AddDays(7);
if (true == bNew)
HttpContext.Current.Response.Cookies.Add(oCookie);
else
HttpContext.Current.Response.Cookies.Set(oCookie);
}
public String GetCookie()
{
String myid = null;
HttpCookie oCookie = HttpContext.Current.Request.Cookies.Get("myData");
if (null != oCookie)
myid = HttpContext.Current.Server.HtmlEncode(oCookie["myid"]);
return myid;
}
思想?
答案 0 :(得分:8)
我做了一些研究,我在另一个stackoverflow.com票证上找到了答案,请参阅here。
基本上,代码更改是:
oCookie.Domain = "mydomain.com";
oCookie.Path = "/";