1 /// <summary>
2 /// Cookie辅助类
3 /// </summary>
4 public class CookieHelper
5 {
6 /// <summary>
7 /// 清除指定Cookie
8 /// </summary>
9 /// <param name="cookiename">cookiename</param>
10 public static void ClearCookie(string cookiename)
11 {
12 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
13 if (cookie != null)
14 {
15 cookie.Expires = DateTime.Now.AddYears(-3);
16 HttpContext.Current.Response.Cookies.Add(cookie);
17 }
18 }
19
20 /// <summary>
21 /// 获取指定Cookie值
22 /// </summary>
23 /// <param name="cookiename">cookiename</param>
24 /// <returns></returns>
25 public static string GetCookieValue(string cookiename)
26 {
27 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
28 string str = string.Empty;
29 if (cookie != null)
30 {
31 str = cookie.Value;
32 }
33 return str;
34 }
35
36 /// <summary>
37 /// 添加一个Cookie(24小时过期)
38 /// </summary>
39 /// <param name="cookiename"></param>
40 /// <param name="cookievalue"></param>
41 public static void SetCookie(string cookiename, string cookievalue)
42 {
43 SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0));
44 }
45
46 /// <summary>
47 /// 添加一个Cookie
48 /// </summary>
49 /// <param name="cookiename">cookie名</param>
50 /// <param name="cookievalue">cookie值</param>
51 /// <param name="expires">过期时间 DateTime</param>
52 public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
53 {
54 HttpCookie cookie = new HttpCookie(cookiename)
55 {
56 Value = cookievalue,
57 Expires = expires
58 };
59 HttpContext.Current.Response.Cookies.Add(cookie);
60 }
61 }