Cookie

Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie。

内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短暂的。硬盘Cookie保存在硬盘里,有一个过期时间,除非用户手工清理或到了过期时间,硬盘Cookie不会被删除,其存在时间是长期的。

Cookie的缺陷

  1. cookie会被附加在每个HTTP请求中,所以无形中增加了流量。
  2. 由于在HTTP请求中的cookie是明文传递的,所以安全性成问题。(除非用HTTPS)
  3. Cookie的大小限制在4KB左右。对于复杂的存储需求来说是不够用的。

ASP.NET C#读写Cookie

 1 /// <summary>
 2 /// 写cookie值
 3 /// </summary>
 4 /// <param name="strName">名称</param>
 5 /// <param name="strValue"></param>
 6 public static void WriteCookie(string strName, string strValue)
 7 {
 8     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 9     if (cookie == null)
10     {
11         cookie = new HttpCookie(strName);
12     }
13     cookie.Value = UrlEncode(strValue);
14     HttpContext.Current.Response.AppendCookie(cookie);
15 }
16 
17 /// <summary>
18 /// 写cookie值
19 /// </summary>
20 /// <param name="strName">名称</param>
21 /// <param name="strValue"></param>
22 /// <param name="expires">过期时间(分钟)</param>
23 public static void WriteCookie(string strName, string strValue, int expires)
24 {
25     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
26     if (cookie == null)
27     {
28         cookie = new HttpCookie(strName);
29     }
30     cookie.Value = UrlEncode(strValue);
31     cookie.Expires = DateTime.Now.AddMinutes(expires);
32     HttpContext.Current.Response.AppendCookie(cookie);
33 }
34 
35 /// <summary>
36 /// 写cookie值
37 /// </summary>
38 /// <param name="strName">名称</param>
39 /// <param name="key"></param>
40 /// <param name="strValue"></param>
41 public static void WriteCookie(string strName, string key, string strValue)
42 {
43     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
44     if (cookie == null)
45     {
46         cookie = new HttpCookie(strName);
47     }
48     cookie[key] = UrlEncode(strValue);
49     HttpContext.Current.Response.AppendCookie(cookie);
50 }
51 
52 /// <summary>
53 /// 写cookie值
54 /// </summary>
55 /// <param name="strName">名称</param>
56 /// <param name="key"></param>
57 /// <param name="strValue"></param>
58 /// <param name="expires">过期时间(分钟)</param>
59 public static void WriteCookie(string strName, string key, string strValue, int expires)
60 {
61     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
62     if (cookie == null)
63     {
64         cookie = new HttpCookie(strName);
65     }
66     cookie[key] = UrlEncode(strValue);
67     cookie.Expires = DateTime.Now.AddMinutes(expires);
68     HttpContext.Current.Response.AppendCookie(cookie);
69 }
70 
71 /// <summary>
72 /// 读cookie值
73 /// </summary>
74 /// <param name="strName">名称</param>
75 /// <returns>cookie值</returns>
76 public static string GetCookie(string strName)
77 {
78     if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
79         return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
80 
81     return "";
82 }
83 
84 /// <summary>
85 /// 读cookie值
86 /// </summary>
87 /// <param name="strName">名称</param>
88 /// <returns>cookie值</returns>
89 public static string GetCookie(string strName, string key)
90 {
91     if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
92         return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
93 
94     return "";
95 }

 

转载于:https://www.cnblogs.com/paullam/p/3754223.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值