C#.net 与 vb.net 中读写 Cookie 的方法!
Cookie (HttpCookie 的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法。例如,当用户访
问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息。当该用户再次访问您的网站时,应用程
序便可以检索以前存储的信息。
C#.net 部分
创建 Cookie 方法 (1)
Response.Cookies["userName"].Value = “admin";
Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);
//如果不设置失效时间,Cookie 信息不会写到用户硬盘,浏览器关闭将会丢弃。
创建 Cookie 方法 (2)
HttpCookie aCookie = new HttpCookie(“lastVisit”); //上一次访问时间
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
访问 Cookie 方法(1)
if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);访问 Cookie 方法(2)
if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
创建多值 Cookie 方法 (1)
Response.Cookies["userInfo"]["userName"] = “admin";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
创建多值 Cookie 方法 (2)
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = “admin";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
读取多值 Cookie
评论0