明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

c#.net与vb.net中读写Cookie的方法!

Posted on   且行且思  阅读(3863)  评论(1编辑  收藏  举报

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


HttpCookie aCookie = Request.Cookies["userInfo"];
string userName=aCookie.Values[“userName”];
string lastVisit=aCookie.Values[“lastVisit”];


vb.net部分

创建Cookie方法 (1)
Response.Cookies("userName").Value = “admin"
Response.Cookies(“userName”).Expires = DateTime.Now.AddDays(1)
'如果不设置失效时间,Cookie信息不会写到用户硬盘,浏览器关闭将会丢弃。
 
 
创建Cookie方法 (2)
Dim aCookie As HttpCookie =  New HttpCookie(“lastVisit”)  '上一次访问时间
aCookie.Value = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
 
 
访问Cookie方法(1)
If Not Request.Cookies("userName") Is Nothing Then
Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)访问Cookie方法(2)
End If
If Not Request.Cookies("userName") Is Nothing Then
Dim aCookie As HttpCookie =  Request.Cookies("userName")
Label1.Text = Server.HtmlEncode(aCookie.Value)
End If
 
 
创建多值Cookie方法 (1)
Response.Cookies("userInfo")("userName") = “admin"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
 
 
创建多值Cookie方法 (2)
Dim aCookie As HttpCookie =  New HttpCookie("userInfo")
aCookie.Values("userName") = “admin"
aCookie.Values("lastVisit") = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
 
 
读取多值Cookie
 
 
Dim aCookie As HttpCookie =  Request.Cookies("userInfo")
Dim userName As String = aCookie.Values(“userName”)
Dim lastVisit As String = aCookie.Values(“lastVisit”)
 
 
修改和删除Cookie
 
不能直接修改或删除Cookie,只能创建一个新的Cookie,发送到客户端以实现修改或删除Cookie.

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示