天天看點

asp.net Cookie跨域、虛拟目錄等設定方法

Cookie有三個屬性需要注意一下:

. Domain 域

. Path 路徑

. Expires 過期時間

跨域操作需要設定域屬性:

Response.Cookies("MyCookie").Domain = "jb51.net"; (這裡指的是泛域名)

這樣在其它二級域名下就都可以通路到了, ASP 和 ASP.NET 測試通過

虛拟目錄下通路:

我在ASP端做了下測試,.NET的沒試, 如果不指定Path屬性, 不同虛拟目錄下Cookie無法共享

将Response.Cookies("MyCookie").Path = "/" 就可以了

總的寫法:

複制代碼 代碼如下:

Response.Cookies("MyCookie").Domain = "jb51.net";

Response.Cookies("MyCookie").Path = "/"

Response.Cookies("MyCookie").Expires = Now + 365;

Response.Cookies("MyCookie")("Test") = "test";

.NET 清除Cookie

複制代碼 代碼如下:

   public void DeleteCookie(string cookiename, string _domain, string _cookiepath)

        {

            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];

            if (cookie != null)

            {

                cookie.Values.Clear();

                SetUserCookieExpireTime(cookiename, -1,,  _domain,  _cookiepath);

                cookie.Domain = _domain;

                System.Web.HttpContext.Current.Response.Cookies.Set(cookie);

            }

        }

        public static void SetUserCookieExpireTime(string key, int days, string _domain, string _cookiepath)

        {

            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, string _cookiepath)

        {

            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域,什麼意思?

預設情況下,Cookie 與特定的域相關聯。例如,如果您的站點是 www.jb51.net,那麼當使用者向該站點請求頁面時,您編寫的 Cookie 就被發送到伺服器。(有特定路徑值的 Cookie 除外。) 如果您的站點有子域(例如 jb51.net、s.jb51.net 和 tools.jb51.net),就可以把 Cookie 同特定的子域相關聯。為此,需要設定 Cookie 的 Domain 屬性,如下所示:

複制代碼 代碼如下:

Response.Cookies("domain").Value = DateTime.Now.ToString

Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)

Response.Cookies("domain").Domain = "s.jb51.net"

如果按照這種方式設定域,則 Cookie 隻能用于指定子域中的頁面。

您也可以利用 Domain 屬性來建立可在多個子域中共享的 Cookie。例如,對域進行如下設定:

複制代碼 代碼如下:

Response.Cookies("domain").Value = DateTime.Now.ToString

Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)

Response.Cookies("domain").Domain = "jb51.net"

這樣,該 Cookie 就可用于主域、s.jb51.net 和 tools.jb51.net。

  //讀取 Cookie 集合

            for (int i = 0; i < Request.Cookies.Count; i++)

            {

                HttpCookie cookies = Request.Cookies[i];

                Response.Write("name=" + cookies.Name + "<br/>");

                if (cookies.HasKeys)//是否有子鍵

                {

                    System.Collections.Specialized.NameValueCollection NameColl

                                                         = cookies.Values;

                    for (int j = 0; j < NameColl.Count; j++)

                    {

                        Response.Write("子鍵名=" + NameColl.AllKeys[j] + "<br/>");

                        Response.Write("子鍵值=" + NameColl[j] + "<br/>");

                    }

                }

                else

                {

                    Response.Write("value=" + cookies.Value + "<br/>");

                }

            }