设置: protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("test", "welcome to bj!");
        hc.Domain = "localhost";
        hc.Expires = DateTime.Now.AddMinutes(15);
        Response.Cookies.Add(hc);      
       
    }
取值://另一页面
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<script>alert('" +Request.Cookies["test"].Value + "');</script>");
    }

解决方案 »

  1.   

    存cookie
    public static void SaveCookie(string CookieName, string CookieValue, double CookieTime)
                {
                    HttpCookie myCookie = new HttpCookie(CookieName);
                    DateTime now = DateTime.Now;
                    myCookie.Value = HttpUtility.UrlEncode(CookieValue);//编码写入转换,防止中文乱码
                    if (CookieTime != 0)
                    {
                        myCookie.Expires = now.AddDays(CookieTime);
                        if (HttpContext.Current.Response.Cookies[CookieName] != null)
                            HttpContext.Current.Response.Cookies.Remove(CookieName);
                        HttpContext.Current.Response.Cookies.Add(myCookie);
                    }
                    else
                    {
                        if (HttpContext.Current.Response.Cookies[CookieName] != null)
                            HttpContext.Current.Response.Cookies.Remove(CookieName);
                        HttpContext.Current.Response.Cookies.Add(myCookie);
                    }
                }
    取Cookiepublic static string GetCookie(string CookieName)
                {
                    HttpCookie myCookie = new HttpCookie(CookieName);
                    myCookie = HttpContext.Current.Request.Cookies[CookieName];
                    if (myCookie != null)
                        return HttpUtility.UrlDecode(myCookie.Value);//返回解码后的Cookie值
                    else
                        return null;
                }
      

  2.   

    存入的时候调用 SaveCookie("test", "welcome to bj!");
    获取的时候 
    GetCookie("test");
      

  3.   

    请问为什么加了hc.Domain = "localhost";这个就取不到值,该怎么去取?
      

  4.   

    跨域读取cookie 你可以看看 
    http://www.cnblogs.com/shchdbk/archive/2010/04/21/1717449.html
      

  5.   

    这个可以换一个域名,怎样取cookie
      

  6.   

     
    function setCookie(name, value, days) {
            if (days) {
               var exp = new Date();
                exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
                document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";domain=.cnonix.com";
            } else {
                document.cookie = name + "=" + escape(value);
           }
        }
        function getCookie(name) {
           var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
           if (arr != null) return unescape(arr[2]); return null;
       }http://www.cnonix.com/cnonixdb/list/0,在这个页面怎么取?
      

  7.   

    我是在http://www.cnonix.com/test.html这个页面添加的,在http://www.cnonix.com/val.html里面取的到,http://www.cnonix.com/cnonixdb/list/0这个页面用 getCookie(name)就取不到了
      

  8.   

    本帖最后由 net_lover 于 2011-10-11 13:49:05 编辑
      

  9.   

    hc.Domain = ".cnonix.com"    写成这样子的不就可以了吗   一般情况下 同一站点 是不加的
      

  10.   

    hc.Domain 这个是跨域的时候才用到的,把它去掉就行了,还有,你这个cookie是干嘛用的,设置的时间会不会有点短;还有,你这个cookie是干嘛用的,设置的时间会不会有点短 /// <summary>
            /// Cookies赋值
            /// </summary>
            /// <param name="strName">主键</param>
            /// <param name="strValue">键值</param>
            /// <param name="strDay">有效天数</param>
            /// <returns></returns>
            public static bool setCookie(string strName, string strValue, int strDay)
            {
                try
                {
                    HttpCookie Cookie = new HttpCookie(strName);
                    //Cookie.Domain = ".zsjdc.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
                    Cookie.Expires = DateTime.Now.AddDays(strDay);
                    //Cookie.Value = Encryption(strValue);
                    Cookie.Value = strValue;
                    System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }        /// <summary>
            /// 读取Cookies
            /// </summary>
            /// <param name="strName">主键</param>
            /// <returns></returns>        public static string getCookie(string strName)
            {
                HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
                if (Cookie != null)
                {
                    //return Decrypt(Cookie.Value.ToString());
                    return Cookie.Value.ToString();
                }
                else
                {
                    return null;
                }
            }用我这个包没问题
      

  11.   

    127.0.0.1就取到了
    localhost 没用
      

  12.   

    我在http://www.cnonix.com/cnonixdb/abc/add加了个cookie,想要到http://www.cnonix.com/index/get这里使用,也就是要在总个网站都可以用
      

  13.   

    你先alert( document.cookie)看你设置的是否存在
      

  14.   

    你究竟是客户端设置Cookie还是服务器端设置的?
      

  15.   

    asp.net设置和读取方法设置
    HttpCookie hc = new HttpCookie("test", Server.UrlEncode("welcome to bj!"));   
    hc.Expires = DateTime.Now.AddMinutes(15);
    hc.Path = "/";
    hc.Secure = false;
    hc.HttpOnly = true;
    Response.Cookies.Add(hc); 
    读取HttpCookie hc = Request.Cookies["test"];
        if (hc == null)
        {
          Response.Write("没有");
        }
        else
        {
          Response.Write(Server.UrlDecode(hc.Value));
        }
      

  16.   

    我的服务端的客户端的都只要加了Domain的都不懂,晕了好久才发现js添加cookie的方法不好用,重写了个就没问题了,服务端的跨域的还不会!