代码 try
        {
            if (Request.Cookies[userdetail.UserID]==null)
            {
                string cookiesName1 = "frameId1";
                string cookiesName2 = "frameTypeId1";
                Response.Cookies[userdetail.UserID][cookiesName1] = frameId;
                Response.Cookies[userdetail.UserID][cookiesName2] = frameTypeId.ToString();
                Response.Cookies[userdetail.UserID].Expires = DateTime.Now.AddDays(7);
            }
            else
            {
                if (Request.Cookies[userdetail.UserID]["frameId2"]==null)
                {
                    string cookiesName1 = "frameId2";
                    string cookiesName2 = "frameTypeId2";
                    Response.Cookies[userdetail.UserID][cookiesName1] = frameId;
                    Response.Cookies[userdetail.UserID][cookiesName2] = frameTypeId.ToString();
                    Response.Cookies[userdetail.UserID].Expires = DateTime.Now.AddDays(7);
                }
                else
                {
                    Response.Cookies[userdetail.UserID]["frameId1"] = Request.Cookies[userdetail.UserID]["frameId2"];
                    Response.Cookies[userdetail.UserID]["frameTypeId1"] = Request.Cookies[userdetail.UserID]["frameTypeId2"];
                    string cookiesName1 = "frameId2";
                    string cookiesName2 = "frameTypeId2";
                    Response.Cookies[userdetail.UserID][cookiesName1] = frameId;
                    Response.Cookies[userdetail.UserID][cookiesName2] = frameTypeId.ToString();
                    Response.Cookies[userdetail.UserID].Expires = DateTime.Now.AddDays(7);
                }
            }
                  
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            throw;
        }
这段代码!  第一次启动程序的时候不存在cookie  当给cookie添加完之后在读取cookie 能够取到cookie的值  但是把程序重新启动   又查不到cookie的值了  这是为什么?  错在哪里了????????

解决方案 »

  1.   


    if (HttpContext.Current.Response.Cookies[userId] != null)
            {
                for (int i = 1; i < 3; i++)
                {
                    string frameId="frameId" + i.ToString();
                    string frameId1 = HttpContext.Current.Response.Cookies[userId][frameId];
                    string frameTypeId1 = HttpContext.Current.Response.Cookies[userId]["frameTypeId" + i.ToString()];
                    HttpContext.Current.Response.Cookies[userId].Expires.AddDays(30);
                    list.Add(new FrameManager().ReadFrame(Convert.ToInt32(frameTypeId1), frameId1));
                }
            }
           这个是读取的方法!~~~~~~~~~~~~
      

  2.   


                        //-------------将用户和密码保存到cookie里
                        HttpCookie bcookie = new HttpCookie("UserInfo");
                        bcookie.Values.Add("User_Name",DBClass.EncryptCookie( tbUserName.Value.Replace(" ", "")));
                        bcookie.Values.Add("User_Pwd", MD5(TextBox_Pwd.Text));
                        bcookie.Values.Add("User_Power", DBClass.EncryptCookie("0"));
                        //--------------读取要保存的时间
                        bcookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(bcookie);                 HttpCookie cookies = Request.Cookies["UserInfo"];
                    Label1.Text = DBClass.DecryptCookie(cookies.Values["User_Name"]);
      

  3.   

    没设置expire,关闭IE,清除cookie
    System.Web.HttpCookie newcookie = new HttpCookie("user");
    newcookie.Values["username"] = "";
    newcookie.Values["password"] = "";
    newcookie.Expires = DateTime.Now.AddDays(15);
    Response.AppendCookie(newcookie);
    System.Web.HttpCookie cookie = Request.Cookies["user"];
    if (cookie != null)
    {
    cookie.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Set(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);  
    }