日期:2014-05-20  浏览次数:20401 次

asp.net中cookie的问题,被整死了,奇了怪了,,,,,,,,,,,,
if (this.chkRemember.Checked == true)//前台页中保存两周复选框勾选
  {
  HttpCookie cookie = new HttpCookie("hluserinfo");

  cookie.Values.Add("hluserid", dr["userID"].ToString());
  cookie.Values.Add("hlusername", dr["username"].ToString());
  cookie.Values.Add("hluserquanxian", dr["userquanxian"].ToString());
  cookie.Expires = DateTime.Now.AddDays(14);//14天有效期
  Response.Cookies.Add(cookie);

  }


登录成功跳转去的页面后台:
  int userid;
  string username;
  int userquanxian;

  protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack)
  {
  HttpCookie mycookies = HttpContext.Current.Request.Cookies["hluserinfo"];
   
  if (mycookies != null) 
  {
  userid = Convert.ToInt32(Response.Cookies["hluserinfo"].Values["hluserid"]);
  username = Response.Cookies["hluserinfo"].Values["hlusername"];
  userquanxian = Convert.ToInt32(Response.Cookies["hluserinfo"].Values["hluserquanxian"]);
  }

  Response.Write(username);
  }
  }

问题是,Response.Write(username);没有任何值,空的,,,,,,,

请问,,,代码有没有写错?如果错了怎么改?

大侠人帮帮我.......


------解决方案--------------------
你打断点调试一下,估计COOkie为空了,获取不到值
------解决方案--------------------
不明白,前面辛辛苦苦的定义了
 HttpCookie mycookies = HttpContext.Current.Request.Cookies["hluserinfo"];
并且做了验证,
后面为什么不用,
却用Response.Cookies。。。。。
------解决方案--------------------
保存cookie用这两个
Response.AppendCookie(cookie);
Response.SetCookie(cookie);
------解决方案--------------------
刚好在做COOIKE的东西

C# code

存入
 HttpCookie mycookie1 = new HttpCookie("abc");
                            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");

                            mycookie1.Values.Add("userName", HttpUtility.UrlEncode(userName.Text, enc));
                            mycookie1.Values.Add("passWord", HttpUtility.UrlEncode(passWord.Text, enc));
                                                           mycookie1.Expires = System.DateTime.Now.AddDays(365);
                                                       Response.Cookies.Add(mycookie1);



读取

 System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");
                    userName.Text = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["abc"].Values["userName"].ToString(),enc);
                    string pass =  HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["abc"].Values["passWord"].ToString(),enc);