打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ASP.NET中的cookie读写方法

Cookie (HttpCookie的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法。例如,当用户访问您的站点时,您可以使用Cookie 存储用户首选项或其他信息。

当该用户再次访问您的网站时,应用程序便可以检索以前存储的信息。

 
  ASP.NET中的cookie:创建Cookie方法 (1)
  Response.Cookies["userName"].Value = “admin";  
  Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);   //如果不设置失效时间,Cookie信息不会写到用户硬盘,浏览器关闭将会丢弃。
 

  ASP.NET中的cookie:创建Cookie方法 (2)
  HttpCookie aCookie = new HttpCookie(“lastVisit”); //上一次访问时间  
  aCookie.Value = DateTime.Now.ToString();  
  aCookie.Expires = DateTime.Now.AddDays(1);  
  Response.Cookies.Add(aCookie); 

 

  ASP.NET中的cookie:访问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);  
  } 

  

  ASP.NET中的cookie:创建多值Cookie方法 (1)
  Response.Cookies["userInfo"]["userName"] = “admin";  
  Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();  
  Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);  ASP.NET中的cookie:创建多值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); 

 

  ASP.NET中的cookie:读取多值Cookie
  HttpCookie aCookie = Request.Cookies["userInfo"];  
  string userName=aCookie.Values[“userName”];  
  string lastVisit=aCookie.Values[“lastVisit”];    ASP.NET中的cookie:修改和删除Cookie
  不能直接修改或删除Cookie,只能创建一个新的Cookie,发送到客户端以实现修改或删除Cookie.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
什么是Cookie?Cookie有什么作用?
【Asp.net】 七大内置对象
MVC5
进攻asp.net - 基于窗体的身份验证 - V6bbs
无 Cookie 的 ASP.NET - Working diary & MySpace ...
ASP.NET安全问题--Froms验证的具体介绍(中篇)(6)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服