1 using System;
2 using System.Web;
3
4 namespace ConsoleApplication5
5 {
6 /// <summary>
7 /// Cookie 助手
8 /// </summary>
9 public sealed class CookieHelper
10 {
11 /// <summary>
12 /// 添加一个 Cookie
13 /// </summary>
14 /// <param name="name">名</param>
15 /// <param name="value">值</param>
16 public static void Add(string name, string value)
17 {
18 var cookie = new HttpCookie(name, value);
19
20 HttpContext.Current.Response.Cookies.Add(cookie);
21 }
22
23 /// <summary>
24 /// 添加一个 Cookie
25 /// </summary>
26 /// <param name="name">名</param>
27 /// <param name="value">值</param>
28 /// <param name="expires">过期日期和时间</param>
29 public static void Add(string name, string value, DateTime expires)
30 {
31 var cookie = new HttpCookie(name, value)
32 {
33 Expires = expires
34 };
35
36 HttpContext.Current.Response.Cookies.Add(cookie);
37 }
38
39 /// <summary>
40 /// 获取 Cookie 值
41 /// </summary>
42 /// <param name="name">名</param>
43 /// <returns></returns>
44 public static string Get(string name)
45 {
46 var cookie = HttpContext.Current.Request.Cookies[name];
47
48 return cookie == null ? string.Empty : cookie.Value;
49 }
50 }
51 }