-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCookieHelper.cs
More file actions
232 lines (214 loc) · 10.8 KB
/
CookieHelper.cs
File metadata and controls
232 lines (214 loc) · 10.8 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* 作用:读取/设置 Cookie 数据。
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
namespace Helper.Core.Library
{
public class CookieHelper
{
#region 对外公开方法
#region 设置 Cookie
/// <summary>
/// 设置 Cookie 数据
/// </summary>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="cookieValue">Cookie 数据</param>
/// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param>
public static void SetCookie(string cookieName, string cookieValue, HttpResponseBase httpResponse = null)
{
SetCookie(cookieName, cookieValue, DateTime.MaxValue, httpResponse);
}
/// <summary>
/// 设置 Cookie 数据
/// </summary>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="cookieValue">Cookie 数据</param>
/// <param name="expires">过期时间</param>
/// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param>
public static void SetCookie(string cookieName, string cookieValue, DateTime expires, HttpResponseBase httpResponse = null)
{
HttpCookie httpCookie = new HttpCookie(cookieName);
httpCookie.HttpOnly = true;
httpCookie.Expires = expires;
httpCookie.Value = cookieValue;
if (httpResponse != null)
{
httpResponse.Cookies.Add(httpCookie);
}
else
{
HttpContext.Current.Response.Cookies.Add(httpCookie);
}
}
/// <summary>
/// 设置 Cookie 数据
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="data">实体数据</param>
/// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param>
/// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="propertyList">属性列表,如果指定,则按指定属性列表设置 Cookie 数据</param>
/// <param name="propertyContain">是否包含,true 属性包含,flase 属性排除</param>
/// <param name="reflectionType">反射类型</param>
public static void SetCookieT<T>(string cookieName, T data, HttpResponseBase httpResponse = null, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
SetCookieT<T>(cookieName, data, DateTime.MaxValue, httpResponse, propertyMatchList, propertyList, propertyContain, reflectionType);
}
/// <summary>
/// 设置 Cookie 数据
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="data">实体数据</param>
/// <param name="expires">过期时间</param>
/// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param>
/// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="propertyList">属性列表,如果指定,则按指定属性列表设置 Cookie 数据</param>
/// <param name="propertyContain">是否包含,true 属性包含,flase 属性排除</param>
/// <param name="reflectionType">反射类型</param>
public static void SetCookieT<T>(string cookieName, T data, DateTime expires, HttpResponseBase httpResponse = null, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
List<string> filterNameList = null;
if (propertyList != null) filterNameList = propertyList.ToList<string>();
Dictionary<string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList);
ExecuteSetCookie<T>(httpResponse, cookieName, data, expires, propertyDict, filterNameList, propertyContain, reflectionType);
}
#endregion
#region 读取 Cookie
/// <summary>
/// 读取 Cookie 数据
/// </summary>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="httpRequest">HttpRequestBase,如果未指定则取自 HttpContext.Current.Request</param>
/// <returns></returns>
public static string GetCookie(string cookieName, HttpRequestBase httpRequest = null)
{
HttpCookie httpCookie = GetHttpCookie(httpRequest, cookieName);
if (httpCookie == null) return null;
return httpCookie.Value;
}
/// <summary>
/// 获取 Cookie 数据
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="httpRequest">HttpRequestBase,如果未指定则取自 HttpContext.Current.Request</param>
/// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static T GetCookieT<T>(string cookieName, HttpRequestBase httpRequest = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class,new()
{
Dictionary<string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList);
return ExecuteGetCookie<T>(httpRequest, cookieName, propertyDict, reflectionType);
}
#endregion
#region 删除 Cookie
/// <summary>
/// 删除 Cookie 数据
/// </summary>
/// <param name="cookieName">Cookie 名称</param>
/// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param>
public static void DeleteCookie(string cookieName, HttpResponseBase httpResponse = null)
{
HttpCookie httpCookie = GetHttpCookie(httpResponse, cookieName);
if (httpCookie == null) return;
httpCookie.Expires = DateTime.Now.AddDays(-1);
}
#endregion
#endregion
#region 逻辑处理私有方法
internal static void ExecuteSetCookie<T>(HttpResponseBase httpResponse, string cookieName, T data, DateTime expires, Dictionary<string, object> propertyDict, List<string> propertyList, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
Dictionary<string, string> cookieNameDict = CommonHelper.InitPropertyWriteMapper<T, CookieTAttribute>(propertyDict, propertyList, propertyContain);
dynamic propertyGetDict = null;
if (reflectionType != ReflectionTypeEnum.Original) propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict<T>(reflectionType);
HttpCookie httpCookie = new HttpCookie(cookieName);
httpCookie.HttpOnly = true;
httpCookie.Expires = expires;
object cookieValue = null;
foreach (KeyValuePair<string, string> keyValueItem in cookieNameDict)
{
if (propertyGetDict != null && propertyGetDict.ContainsKey(keyValueItem.Value))
{
cookieValue = propertyGetDict[keyValueItem.Value](data);
}
else
{
cookieValue = ReflectionHelper.GetPropertyValue(data, keyValueItem.Value);
}
if (cookieValue != null)
{
httpCookie.Values.Add(keyValueItem.Key, HttpUtility.UrlEncode(cookieValue.ToString()));
}
}
if (httpResponse != null)
{
httpResponse.Cookies.Add(httpCookie);
}
else
{
HttpContext.Current.Response.Cookies.Add(httpCookie);
}
}
internal static T ExecuteGetCookie<T>(HttpRequestBase httpRequest, string cookieName, Dictionary<string, object> propertyDict, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class,new()
{
HttpCookie httpCookie = GetHttpCookie(httpRequest, cookieName);
if (httpCookie == null) return null;
Dictionary<PropertyInfo, string> mapperDict = CommonHelper.InitPropertyReadMapper<T, CookieTAttribute>(propertyDict, (name) => httpCookie.Values[name] != null);
dynamic propertySetDict = null;
if (reflectionType != ReflectionTypeEnum.Original) propertySetDict = ReflectionExtendHelper.PropertySetCallDict<T>(reflectionType);
T t = ReflectionGenericHelper.New<T>();
foreach (var keyValueItem in mapperDict)
{
if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name))
{
ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, HttpUtility.UrlDecode(httpCookie[keyValueItem.Value]), keyValueItem.Key);
}
else
{
ReflectionHelper.SetPropertyValue(t, HttpUtility.UrlDecode(httpCookie[keyValueItem.Value]), keyValueItem.Key);
}
}
return t;
}
private static HttpCookie GetHttpCookie(HttpResponseBase httpResponse, string cookieName)
{
if(httpResponse != null)
{
return httpResponse.Cookies[cookieName];
}
else
{
return HttpContext.Current.Response.Cookies[cookieName];
}
}
private static HttpCookie GetHttpCookie(HttpRequestBase httpRequest, string cookieName)
{
if (httpRequest != null)
{
return httpRequest.Cookies[cookieName];
}
else
{
return HttpContext.Current.Request.Cookies[cookieName];
}
}
#endregion
}
#region 逻辑处理辅助特性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class CookieTAttribute : BaseReadAndWriteTAttribute
{
public CookieTAttribute(string name, AttributeReadAndWriteTypeEnum type = AttributeReadAndWriteTypeEnum.ReadAndWrite)
: base(name, type)
{
}
}
#endregion
}