给定:
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;
}
你有什么想法?