[C#] 簡單的 Helper 封裝 -- CookieHelper


 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM