[英]Accessing a cookie created on one subdomain on another subdomain
鑒於:
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;
}
思考?
我做了一些更多的研究,我在另一個stackoverflow.com票上找到了答案,請看這里 。
基本上,代碼更改是:
oCookie.Domain = "mydomain.com";
oCookie.Path = "/";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.