ASP.Net 后台访问Cookie的封装


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

这是一个类,用于提供后台C#访问Cookie,函数不多,只有几个,包括设置、获取、修改。具体代码如下:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
 
namespace SqlHelper {
	public class Cookie2 {
		/// <summary>
		/// 设置Cookie的值,关闭Session后释放
		/// </summary>
		/// <param name="name">名称</param>
		/// <param name="value">Cookie值</param>
		/// <param name="response">HttpResponse</param>
		public static void Set (HttpResponse response, string name, string value) {
			HttpCookie cookie = new HttpCookie (name);
			cookie.Values.Add (name, value);
			response.AppendCookie (cookie);
		}
 
 
 
		/// <summary>
		/// 设置Cookie的值,包含过期时间
		/// </summary>
		/// <param name="name">名称</param>
		/// <param name="value">Cookie值</param>
		/// <param name="expiredays">过期时间</param>
		/// <param name="response">HttpResponse</param>
		public static void Set (HttpResponse response, string name, string value, DateTime expiredays) {
			HttpCookie cookie = new HttpCookie (name);
			cookie.Expires = expiredays;
			cookie.Values.Add (name, value);
			response.AppendCookie (cookie);
		}
 
 
 
		/// <summary>
		/// 获取Cookie值
		/// </summary>
		/// <param name="name">名称</param>
		/// <param name="request">HttpRequest</param>
		/// <returns>Cookie的值</returns>
		public static string Get (HttpRequest request, string name) {
			if (request.Cookies[name] != null) {
				return request.Cookies[name].Values.toString();
			} else {
				return null;
			}
		}
 
 
 
		/// <summary>
		/// 删除Cookie
		/// </summary>
		/// <param name="name">名称</param>
		/// <param name="response">HttpResponse</param>
		/// <param name="request">HttpRequest</param>
		public static void Delete (HttpResponse response, HttpRequest request, string name) {
			HttpCookie cookie = request.Cookies[name];
			if (cookie != null) {
				cookie.Expires = DateTime.Now.AddDays (-1);
				response.AppendCookie (cookie);
			}
		}
 
 
 
		/// <summary>
		/// 修改Cookie的值
		/// </summary>
		/// <param name="name">名称</param>
		/// <param name="value">Cookie值</param>
		/// <param name="response">HttpResponse</param>
		/// <param name="request">HttpRequest</param>
		public static void Modify (HttpResponse response, HttpRequest request, string name, string value) {
			HttpCookie cookie = request.Cookies[name];
			if (cookie != null) {
				cookie.Values[name] = value;
			}
			response.AppendCookie (cookie);
		}
	}
}

发布者

fawdlstty

又一只萌萌哒程序猿~~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注