原文地址:http://www.cnblogs.com/greatverve/archive/2011/07/05/asp-net-cookie-domain.html
將Cookie的有效範圍限制到域。
預設情況下,Cookie 與特定的域相關聯。
例如,如果您的網站是 www.contoso.com,那麼當使用者向該網站請求頁面時,
您編寫的Cookie就被發送到伺服器。(有特定路徑值的Cookie除外)
如果您的網站有子域(例如 contoso.com、sales.contoso.com 和 support.contoso.com),
就可以把Cookie同特定的子域相關聯。為此,需要設定Cookie的 Domain 屬性,如下所示: Response.Cookies( "domain ").Value = DateTime.Now.ToString
Response.Cookies( "domain ").Expires = DateTime.Now.AddDays(1)
Response.Cookies( "domain ").Domain = "support.contoso.com "
如果按照這種方式設定域,則Cookie只能用於指定子域中的頁面。
您也可以利用Domain屬性來建立可在多個子域中共用的Cookie。例如,對域進行如下設定: Response.Cookies( "domain ").Value = DateTime.Now.ToString
Response.Cookies( "domain ").Expires = DateTime.Now.AddDays(1)
Response.Cookies( "domain ").Domain = "contoso.com "
這樣,該 Cookie 就可用於主域、sales.contoso.com 和 support.contoso.com。
以下是建立一個跨域的Cookie,只能實現同一個根域下的Cookie
如:www.it100.info,在這個根域下的所有次層網域可共用Cookie,{mail.it100.info,photo.it100.info} public static bool CreateCookie(string strCookieName, string strCookieValue, string strDomain, bool blURLEncode)
{
if (blURLEncode)
{
strCookieValue = System.Web.HttpContext.Current.Server.UrlEncode(strCookieValue);
}
HttpCookie objCookie = new HttpCookie(strCookieName, strCookieValue);
objCookie.Domain = strDomain; //設定Cookie的網域名稱
System.Web.HttpContext.Current.Response.Cookies.Add(objCookie);
return true;
}
url:http://greatverve.cnblogs.com/archive/2011/07/05/asp-net-cookie-domain.html
Cookie有三個屬性需要注意一下:
1. Domain 域
2. Path 路徑
3. Expires 到期時間
跨網域作業需要設定域屬性:
Response.Cookies("MyCookie").Domain = "cnblogs.com"; (這裡指的是泛網域名稱)
這樣在其它次層網域下就都可以訪問到了, ASP 和 ASP.NET 測試通過
虛擬目錄下訪問:
我在ASP端做了下測試,.NET的沒試, 如果不指定Path屬性, 不同虛擬目錄下Cookie無法共用
將Response.Cookies("MyCookie").Path = "/" 就可以了
總的寫法:
Response.Cookies("MyCookie").Domain = "cnblogs.com";
Response.Cookies("MyCookie").Path = "/"
Response.Cookies("MyCookie").Expires = Now + 365;
Response.Cookies("MyCookie")("Test") = "test";
.NET 清除Cookie
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Values.Clear();
SetUserCookieExpireTime(cookiename, -1);
cookie.Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
}
public static void SetUserCookieExpireTime(string key, int days)
{
System.Web.HttpContext.Current.Response.Cookies[key].Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies[key].Path = _cookiepath;
System.Web.HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(days);
}
.NET 添加/更新Cookie
public static void AddUserCookies(string key,string value, string cookiename, string domain)
{
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = domain;
cookie.Path = _cookiepath;
cookie.Values.Add(key, value);
HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
if (System.Web.HttpContext.Current.Request.Cookies[cookiename].Values[key] != null)
{
cookie.Values.Set(key, value);
}
else
{
cookie.Domain = domain;
cookie.Path = _cookiepath;
cookie.Values.Add(key, value);
HttpContext.Current.Response.AppendCookie(cookie);
}
}
}
這種寫法實現cookie跨域跨目錄