Cookie对象的使用
写入Cookie的方法
方法1
Response.Cookies["userName"].Value = "patrick"; Response.Cookies["userName"].Expires = DateTime.Now.AddDays(5);
方法2
HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.Value = Server.HtmlEncode("一个Cookie应用程序");//设置Cookie的值 MyCookie.Expires = DateTime.Now.AddDays(5);//设置Cookie过期时间 Response.Cookies.Add(MyCookie);//新增Cookie
方法3
HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.Value = Server.HtmlEncode("一个Cookie应用程序");//设置Cookie的值 MyCookie.Expires = DateTime.Now.AddDays(5);//设置Cookie过期时间 Response.AppendCookie(MyCookie);
获取名为username的Cookie的值,并将其像是在label控件中
方法1
if (Request.Cookies["username"] != null) label1.Text = Server.HtmlEncode(Request.Cookies["username"].Value);
方法2
if (Request.Cookies["username"] != null) { HttpCookie MyCookie = Request.Cookies["username"]; label1.Text = Server.HtmlEncode(MyCookie.Value); }