在客户端创建一个username的cookies,其值为zym,有效期为1天
方法1:
Response.Cookies["username"].Value = "zym";
Response.Cookies["username"].Expires = DateTime.Now.AddDays(1);//有效期
方法2:
System.Web.HttpCookie coo = new HttpCookie("username", "zym");
coo.Expires = DateTime.Now.AddDays(1);
Response.AppendCookie(coo);
创建带有子键的cookies:
System.Web.HttpCookie newcoo = new HttpCookie("user");
newcoo.Values["username"] = "zym";
newcoo.Values["password"] = "123";
newcoo.Expires = DateTime.Now.AddDays(1);
Response.AppendCookie(newcoo);
cookies的读取:
无子键读取:
if (Request.Cookies["user"] != null)
Response.Write(Server.HtmlEncode(Request.Cookies["username"].Value));
有子键读取:
if (Request.Cookies["username"] != null)
{
Response.Write(Server.HtmlEncode(Request.Cookies["user"] ["username"].Value));
Response.Write(Server.HtmlEncode(Request.Cookies["user"] ["password"].Value));
}