.net cookie应用

来源:百度知道 编辑:UC知道 时间:2024/05/16 07:38:21
HttpCookie ck = new HttpCookie("userInfo");
ck.Expires = Convert.ToDateTime("2012-12-31 0:0");
ck.Values.Add("username", txtUserName.Text);
明明Expires己经设置很久了,为什么还是关闭浏览器就失效?

cookie不是这样写入的 ck.Values是只读的,没法赋值

HttpContext curr = HttpContext.Current;
HttpCookie cookie = new HttpCookie("username", txtUserName.Text);
cookie.Expires = DateTime.Now.AddYear(1);
curr.Response.Cookies.Add(cookie);

试试这个代码
你主要是这句没写吧 this.Response.Cookies.Add(hc);

在这页面加载事件中写
if(!this.IsPostBack)
{
if(this.Request.Cookies["userInfo"]!=null)
{
this.txtUserName.Text=this.Request.Cookies["userInfo"].Value.
ToString();
}
}

设置Cookie
HttpCookie hc=new HttpCookie("userInfo",this.txtUserName.Text);
hc.Expires=DateTime.Now.addDays(7);
this.Response.Cookies.Add(hc);

给你参考下哈 >_<