3
3
using Microsoft . Extensions . Options ;
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
7
- using System . Threading . Tasks ;
8
6
9
7
namespace CookieManager
10
8
{
@@ -13,25 +11,25 @@ namespace CookieManager
13
11
/// </summary>
14
12
public class HttpCookie : ICookie
15
13
{
16
- private readonly HttpContext _httpContext ;
14
+ private readonly IHttpContextAccessor _httpContextAccessor ;
17
15
private readonly IDataProtector _dataProtector ;
18
16
private static readonly string Purpose = "CookieManager.Token.v1" ;
19
17
private readonly CookieManagerOptions _cookieManagerOptions ;
20
18
private readonly ChunkingHttpCookie _chunkingHttpCookie ;
21
19
22
20
23
21
24
- /// <summary>
25
- /// External depedenacy of <see cref="IHttpContextAccessor" />
26
- /// </summary>
27
- /// <param name="httpAccessor ">IHttpAccessor</param>
28
- /// <param name="dataProtectionProvider">data protection provider</param>
29
- /// <param name="optionAccessor">cookie manager option accessor</param>
30
- public HttpCookie ( IHttpContextAccessor httpAccessor ,
22
+ /// <summary>
23
+ /// External depedenacy of <see cref="IHttpContextAccessor" />
24
+ /// </summary>
25
+ /// <param name="httpContextAccessor ">IHttpAccessor</param>
26
+ /// <param name="dataProtectionProvider">data protection provider</param>
27
+ /// <param name="optionAccessor">cookie manager option accessor</param>
28
+ public HttpCookie ( IHttpContextAccessor httpContextAccessor ,
31
29
IDataProtectionProvider dataProtectionProvider ,
32
30
IOptions < CookieManagerOptions > optionAccessor )
33
31
{
34
- _httpContext = httpAccessor . HttpContext ;
32
+ _httpContextAccessor = httpContextAccessor ;
35
33
_dataProtector = dataProtectionProvider . CreateProtector ( Purpose ) ;
36
34
_cookieManagerOptions = optionAccessor . Value ;
37
35
_chunkingHttpCookie = new ChunkingHttpCookie ( optionAccessor ) ;
@@ -41,28 +39,28 @@ public ICollection<string> Keys
41
39
{
42
40
get
43
41
{
44
- if ( _httpContext == null )
42
+ if ( _httpContextAccessor . HttpContext == null )
45
43
{
46
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
44
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
47
45
}
48
46
49
- return _httpContext . Request . Cookies . Keys ;
47
+ return _httpContextAccessor . HttpContext . Request . Cookies . Keys ;
50
48
}
51
49
}
52
50
53
51
public bool Contains ( string key )
54
52
{
55
- if ( _httpContext == null )
53
+ if ( _httpContextAccessor . HttpContext == null )
56
54
{
57
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
55
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
58
56
}
59
57
60
58
if ( key == null )
61
59
{
62
60
throw new ArgumentNullException ( nameof ( key ) ) ;
63
61
}
64
62
65
- return _httpContext . Request . Cookies . ContainsKey ( key ) ;
63
+ return _httpContextAccessor . HttpContext . Request . Cookies . ContainsKey ( key ) ;
66
64
}
67
65
68
66
/// <summary>
@@ -72,9 +70,9 @@ public bool Contains(string key)
72
70
/// <returns>value</returns>
73
71
public string Get ( string key )
74
72
{
75
- if ( _httpContext == null )
73
+ if ( _httpContextAccessor . HttpContext == null )
76
74
{
77
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
75
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
78
76
}
79
77
80
78
if ( key == null )
@@ -84,7 +82,7 @@ public string Get(string key)
84
82
85
83
if ( Contains ( key ) )
86
84
{
87
- var encodedValue = _chunkingHttpCookie . GetRequestCookie ( _httpContext , key ) ;
85
+ var encodedValue = _chunkingHttpCookie . GetRequestCookie ( _httpContextAccessor . HttpContext , key ) ;
88
86
var protectedData = string . Empty ;
89
87
//allow encryption is optional
90
88
//may change the allow encryption to avoid this first check if cookie value is able to decode than unprotect tha data
@@ -108,17 +106,17 @@ public string Get(string key)
108
106
/// <param name="key">Key</param>
109
107
public void Remove ( string key )
110
108
{
111
- if ( _httpContext == null )
109
+ if ( _httpContextAccessor . HttpContext == null )
112
110
{
113
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
111
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
114
112
}
115
113
116
114
if ( key == null )
117
115
{
118
116
throw new ArgumentNullException ( nameof ( key ) ) ;
119
117
}
120
118
121
- _chunkingHttpCookie . RemoveCookie ( _httpContext , key ) ;
119
+ _chunkingHttpCookie . RemoveCookie ( _httpContextAccessor . HttpContext , key ) ;
122
120
}
123
121
124
122
/// <summary>
@@ -129,15 +127,15 @@ public void Remove(string key)
129
127
/// <param name="expireTime">Expire time (default time is 10 millisencond)</param>
130
128
public void Set ( string key , string value , int ? expireTime )
131
129
{
132
- //validate input TODO
133
- if ( _httpContext == null )
130
+ //validate input
131
+ if ( _httpContextAccessor . HttpContext == null )
134
132
{
135
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
133
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
136
134
}
137
135
138
- if ( key == null )
136
+ if ( string . IsNullOrEmpty ( key ) )
139
137
{
140
- throw new ArgumentNullException ( nameof ( key ) ) ;
138
+ throw new ArgumentException ( nameof ( key ) ) ;
141
139
}
142
140
143
141
Set ( key , value , null , expireTime ) ;
@@ -151,9 +149,9 @@ public void Set(string key, string value, int? expireTime)
151
149
/// <param name="option">CookieOption</param>
152
150
public void Set ( string key , string value , CookieOptions option )
153
151
{
154
- if ( _httpContext == null )
152
+ if ( _httpContextAccessor . HttpContext == null )
155
153
{
156
- throw new ArgumentNullException ( nameof ( _httpContext ) ) ;
154
+ throw new ArgumentNullException ( nameof ( _httpContextAccessor . HttpContext ) ) ;
157
155
}
158
156
159
157
if ( key == null )
@@ -186,12 +184,12 @@ private void Set(string key, string value, CookieOptions option, int? expireTime
186
184
{
187
185
string protecetedData = _dataProtector . Protect ( value ) ;
188
186
var encodedValue = Base64TextEncoder . Encode ( protecetedData ) ;
189
- _chunkingHttpCookie . AppendResponseCookie ( _httpContext , key , encodedValue , option ) ;
187
+ _chunkingHttpCookie . AppendResponseCookie ( _httpContextAccessor . HttpContext , key , encodedValue , option ) ;
190
188
}
191
189
else
192
190
{
193
191
//just append the cookie
194
- _chunkingHttpCookie . AppendResponseCookie ( _httpContext , key , value , option ) ;
192
+ _chunkingHttpCookie . AppendResponseCookie ( _httpContextAccessor . HttpContext , key , value , option ) ;
195
193
}
196
194
197
195
}
0 commit comments