Skip to content

Commit d434dda

Browse files
committed
Fix for use of httpcontext
1 parent 903fd9d commit d434dda

File tree

4 files changed

+113
-42
lines changed

4 files changed

+113
-42
lines changed

src/CookieManager/CookieManager.csproj

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<!--<TargetFramework>netcoreapp1.0</TargetFramework>-->
54
<TargetFrameworks>netstandard1.6;netstandard2.0</TargetFrameworks>
65
<RepositoryUrl>https://github.com/nemi-chand/CookieManager</RepositoryUrl>
76
<PackageProjectUrl>https://github.com/nemi-chand/CookieManager</PackageProjectUrl>
87
<Description>ASP.Net Core Abstraction layer on top of Http Cookie</Description>
98
<Authors>Nemi Chand</Authors>
109
<Title>CookieManager : ASP.Net Core Abstraction layer on top of Http Cookie</Title>
11-
<Version>2.0.0</Version>
10+
<Version>2.0.1</Version>
1211
<NeutralLanguage>en</NeutralLanguage>
1312
<PackageTags>Cookie Manager,asp.net core cookie,cookie,http cookie,secure cookie</PackageTags>
14-
<PackageReleaseNotes>Added support netstandard 1.6 and netstandard 2.0</PackageReleaseNotes>
15-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
16-
<FileVersion>2.0.0.0</FileVersion>
13+
<PackageReleaseNotes>Bux fix related to httpcontext</PackageReleaseNotes>
14+
<AssemblyVersion>2.0.1.0</AssemblyVersion>
15+
<FileVersion>2.0.1.0</FileVersion>
1716
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
18-
<PackageLicenseUrl>https://github.com/nemi-chand/CookieManager/blob/master/LICENSE</PackageLicenseUrl>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1919
</PropertyGroup>
2020

2121
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
@@ -31,8 +31,10 @@
3131
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.0.0" />
3232
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
3333
</ItemGroup>
34-
3534

35+
<ItemGroup>
36+
<None Include="docs\README.md" Pack="true" PackagePath="\"/>
37+
</ItemGroup>
3638

3739
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
3840
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="2.0.0" />

src/CookieManager/HttpCookie.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using Microsoft.Extensions.Options;
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Threading.Tasks;
86

97
namespace CookieManager
108
{
@@ -13,25 +11,25 @@ namespace CookieManager
1311
/// </summary>
1412
public class HttpCookie : ICookie
1513
{
16-
private readonly HttpContext _httpContext;
14+
private readonly IHttpContextAccessor _httpContextAccessor;
1715
private readonly IDataProtector _dataProtector;
1816
private static readonly string Purpose = "CookieManager.Token.v1";
1917
private readonly CookieManagerOptions _cookieManagerOptions;
2018
private readonly ChunkingHttpCookie _chunkingHttpCookie;
2119

2220

2321

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,
3129
IDataProtectionProvider dataProtectionProvider,
3230
IOptions<CookieManagerOptions> optionAccessor)
3331
{
34-
_httpContext = httpAccessor.HttpContext;
32+
_httpContextAccessor = httpContextAccessor;
3533
_dataProtector = dataProtectionProvider.CreateProtector(Purpose);
3634
_cookieManagerOptions = optionAccessor.Value;
3735
_chunkingHttpCookie = new ChunkingHttpCookie(optionAccessor);
@@ -41,28 +39,28 @@ public ICollection<string> Keys
4139
{
4240
get
4341
{
44-
if(_httpContext == null)
42+
if(_httpContextAccessor.HttpContext == null)
4543
{
46-
throw new ArgumentNullException(nameof(_httpContext));
44+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
4745
}
4846

49-
return _httpContext.Request.Cookies.Keys;
47+
return _httpContextAccessor.HttpContext.Request.Cookies.Keys;
5048
}
5149
}
5250

5351
public bool Contains(string key)
5452
{
55-
if(_httpContext == null)
53+
if(_httpContextAccessor.HttpContext == null)
5654
{
57-
throw new ArgumentNullException(nameof(_httpContext));
55+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
5856
}
5957

6058
if(key == null)
6159
{
6260
throw new ArgumentNullException(nameof(key));
6361
}
6462

65-
return _httpContext.Request.Cookies.ContainsKey(key);
63+
return _httpContextAccessor.HttpContext.Request.Cookies.ContainsKey(key);
6664
}
6765

6866
/// <summary>
@@ -72,9 +70,9 @@ public bool Contains(string key)
7270
/// <returns>value</returns>
7371
public string Get(string key)
7472
{
75-
if (_httpContext == null)
73+
if (_httpContextAccessor.HttpContext == null)
7674
{
77-
throw new ArgumentNullException(nameof(_httpContext));
75+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
7876
}
7977

8078
if (key == null)
@@ -84,7 +82,7 @@ public string Get(string key)
8482

8583
if (Contains(key))
8684
{
87-
var encodedValue = _chunkingHttpCookie.GetRequestCookie(_httpContext, key);
85+
var encodedValue = _chunkingHttpCookie.GetRequestCookie(_httpContextAccessor.HttpContext, key);
8886
var protectedData = string.Empty;
8987
//allow encryption is optional
9088
//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)
108106
/// <param name="key">Key</param>
109107
public void Remove(string key)
110108
{
111-
if (_httpContext == null)
109+
if (_httpContextAccessor.HttpContext == null)
112110
{
113-
throw new ArgumentNullException(nameof(_httpContext));
111+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
114112
}
115113

116114
if (key == null)
117115
{
118116
throw new ArgumentNullException(nameof(key));
119117
}
120118

121-
_chunkingHttpCookie.RemoveCookie(_httpContext, key);
119+
_chunkingHttpCookie.RemoveCookie(_httpContextAccessor.HttpContext, key);
122120
}
123121

124122
/// <summary>
@@ -129,15 +127,15 @@ public void Remove(string key)
129127
/// <param name="expireTime">Expire time (default time is 10 millisencond)</param>
130128
public void Set(string key, string value, int? expireTime)
131129
{
132-
//validate input TODO
133-
if (_httpContext == null)
130+
//validate input
131+
if (_httpContextAccessor.HttpContext == null)
134132
{
135-
throw new ArgumentNullException(nameof(_httpContext));
133+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
136134
}
137135

138-
if (key == null)
136+
if (string.IsNullOrEmpty(key))
139137
{
140-
throw new ArgumentNullException(nameof(key));
138+
throw new ArgumentException(nameof(key));
141139
}
142140

143141
Set(key, value, null, expireTime);
@@ -151,9 +149,9 @@ public void Set(string key, string value, int? expireTime)
151149
/// <param name="option">CookieOption</param>
152150
public void Set(string key, string value, CookieOptions option)
153151
{
154-
if(_httpContext == null)
152+
if(_httpContextAccessor.HttpContext == null)
155153
{
156-
throw new ArgumentNullException(nameof(_httpContext));
154+
throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext));
157155
}
158156

159157
if(key == null)
@@ -186,12 +184,12 @@ private void Set(string key, string value, CookieOptions option, int? expireTime
186184
{
187185
string protecetedData = _dataProtector.Protect(value);
188186
var encodedValue = Base64TextEncoder.Encode(protecetedData);
189-
_chunkingHttpCookie.AppendResponseCookie(_httpContext, key, encodedValue, option);
187+
_chunkingHttpCookie.AppendResponseCookie(_httpContextAccessor.HttpContext, key, encodedValue, option);
190188
}
191189
else
192190
{
193191
//just append the cookie
194-
_chunkingHttpCookie.AppendResponseCookie(_httpContext, key, value, option);
192+
_chunkingHttpCookie.AppendResponseCookie(_httpContextAccessor.HttpContext, key, value, option);
195193
}
196194

197195
}

src/CookieManager/ICookieManager.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using Microsoft.AspNetCore.Http;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63

74
namespace CookieManager
85
{

src/CookieManager/docs/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Cookie Manager Usages
2+
3+
### ICookieManager interface
4+
5+
```csharp
6+
public class MyCookie
7+
{
8+
public string Id { get; set; }
9+
10+
public DateTime Date { get; set; }
11+
12+
public string Indentifier { get; set; }
13+
}
14+
15+
// Get the myCookie object
16+
MyCookie objFromCookie = _cookieManager.Get<MyCookie>("Key");
17+
18+
// Set the myCookie object
19+
MyCookie cooObj= new MyCookie()
20+
{
21+
Id = Guid.NewGuid().ToString(),
22+
Indentifier = "valueasgrsdgdf66514sdfgsd51d65s31g5dsg1rs5dg",
23+
Date = DateTime.Now
24+
};
25+
_cookieManager.Set("Key", cooObj, 60);
26+
27+
// Get or set <T>
28+
// CookieOption example
29+
MyCookie myCook = _cookieManager.GetOrSet<MyCookie>("Key", () =>
30+
{
31+
// Write function to store output in cookie
32+
return new MyCookie()
33+
{
34+
Id = Guid.NewGuid().ToString(),
35+
Indentifier = "valueasgrsdgdf66514sdfgsd51d65s31g5dsg1rs5dg",
36+
Date = DateTime.Now
37+
};
38+
39+
}, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(1) });
40+
41+
```
42+
### ICookie interface
43+
44+
```csharp
45+
// Gets a cookie item associated with key
46+
_cookie.Get("Key");
47+
48+
// Sets the cookie
49+
_cookie.Set("Key", "value here", new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(1) });
50+
51+
```
52+
53+
### Configure Option
54+
Add CookieManager in startup class in Configure Service
55+
```csharp
56+
// Add CookieManager
57+
services.AddCookieManager();
58+
59+
// or
60+
61+
// Add CookieManager with options
62+
services.AddCookieManager(options =>
63+
{
64+
// Allow cookie data to encrypt by default it allow encryption
65+
options.AllowEncryption = false;
66+
// Throw if not all chunks of a cookie are available on a request for re-assembly.
67+
options.ThrowForPartialCookies = true;
68+
// Set null if not allow to devide in chunks
69+
options.ChunkSize = null;
70+
// Default Cookie expire time if expire time set to null of cookie
71+
// Default time is 1 day to expire cookie
72+
options.DefaultExpireTimeInDays = 10;
73+
});
74+
```

0 commit comments

Comments
 (0)