From d434dda3377cfa602d698cb9ff23c1159c65f0a6 Mon Sep 17 00:00:00 2001 From: nemi-chand Date: Wed, 8 Mar 2023 17:34:17 +0530 Subject: [PATCH] Fix for use of httpcontext --- src/CookieManager/CookieManager.csproj | 16 +++--- src/CookieManager/HttpCookie.cs | 62 +++++++++++---------- src/CookieManager/ICookieManager.cs | 3 -- src/CookieManager/docs/README.md | 74 ++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 42 deletions(-) create mode 100644 src/CookieManager/docs/README.md diff --git a/src/CookieManager/CookieManager.csproj b/src/CookieManager/CookieManager.csproj index 667fef4..a9ca4e9 100644 --- a/src/CookieManager/CookieManager.csproj +++ b/src/CookieManager/CookieManager.csproj @@ -1,21 +1,21 @@  - netstandard1.6;netstandard2.0 https://github.com/nemi-chand/CookieManager https://github.com/nemi-chand/CookieManager ASP.Net Core Abstraction layer on top of Http Cookie Nemi Chand CookieManager : ASP.Net Core Abstraction layer on top of Http Cookie - 2.0.0 + 2.0.1 en Cookie Manager,asp.net core cookie,cookie,http cookie,secure cookie - Added support netstandard 1.6 and netstandard 2.0 - 2.0.0.0 - 2.0.0.0 + Bux fix related to httpcontext + 2.0.1.0 + 2.0.1.0 true - https://github.com/nemi-chand/CookieManager/blob/master/LICENSE + README.md + Apache-2.0 @@ -31,8 +31,10 @@ - + + + diff --git a/src/CookieManager/HttpCookie.cs b/src/CookieManager/HttpCookie.cs index 4674ce7..a3040d8 100644 --- a/src/CookieManager/HttpCookie.cs +++ b/src/CookieManager/HttpCookie.cs @@ -3,8 +3,6 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace CookieManager { @@ -13,7 +11,7 @@ namespace CookieManager /// public class HttpCookie : ICookie { - private readonly HttpContext _httpContext; + private readonly IHttpContextAccessor _httpContextAccessor; private readonly IDataProtector _dataProtector; private static readonly string Purpose = "CookieManager.Token.v1"; private readonly CookieManagerOptions _cookieManagerOptions; @@ -21,17 +19,17 @@ public class HttpCookie : ICookie - /// - /// External depedenacy of - /// - /// IHttpAccessor - /// data protection provider - /// cookie manager option accessor - public HttpCookie(IHttpContextAccessor httpAccessor, + /// + /// External depedenacy of + /// + /// IHttpAccessor + /// data protection provider + /// cookie manager option accessor + public HttpCookie(IHttpContextAccessor httpContextAccessor, IDataProtectionProvider dataProtectionProvider, IOptions optionAccessor) { - _httpContext = httpAccessor.HttpContext; + _httpContextAccessor = httpContextAccessor; _dataProtector = dataProtectionProvider.CreateProtector(Purpose); _cookieManagerOptions = optionAccessor.Value; _chunkingHttpCookie = new ChunkingHttpCookie(optionAccessor); @@ -41,20 +39,20 @@ public ICollection Keys { get { - if(_httpContext == null) + if(_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } - return _httpContext.Request.Cookies.Keys; + return _httpContextAccessor.HttpContext.Request.Cookies.Keys; } } public bool Contains(string key) { - if(_httpContext == null) + if(_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } if(key == null) @@ -62,7 +60,7 @@ public bool Contains(string key) throw new ArgumentNullException(nameof(key)); } - return _httpContext.Request.Cookies.ContainsKey(key); + return _httpContextAccessor.HttpContext.Request.Cookies.ContainsKey(key); } /// @@ -72,9 +70,9 @@ public bool Contains(string key) /// value public string Get(string key) { - if (_httpContext == null) + if (_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } if (key == null) @@ -84,7 +82,7 @@ public string Get(string key) if (Contains(key)) { - var encodedValue = _chunkingHttpCookie.GetRequestCookie(_httpContext, key); + var encodedValue = _chunkingHttpCookie.GetRequestCookie(_httpContextAccessor.HttpContext, key); var protectedData = string.Empty; //allow encryption is optional //may change the allow encryption to avoid this first check if cookie value is able to decode than unprotect tha data @@ -108,9 +106,9 @@ public string Get(string key) /// Key public void Remove(string key) { - if (_httpContext == null) + if (_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } if (key == null) @@ -118,7 +116,7 @@ public void Remove(string key) throw new ArgumentNullException(nameof(key)); } - _chunkingHttpCookie.RemoveCookie(_httpContext, key); + _chunkingHttpCookie.RemoveCookie(_httpContextAccessor.HttpContext, key); } /// @@ -129,15 +127,15 @@ public void Remove(string key) /// Expire time (default time is 10 millisencond) public void Set(string key, string value, int? expireTime) { - //validate input TODO - if (_httpContext == null) + //validate input + if (_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } - if (key == null) + if (string.IsNullOrEmpty(key)) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentException(nameof(key)); } Set(key, value, null, expireTime); @@ -151,9 +149,9 @@ public void Set(string key, string value, int? expireTime) /// CookieOption public void Set(string key, string value, CookieOptions option) { - if(_httpContext == null) + if(_httpContextAccessor.HttpContext == null) { - throw new ArgumentNullException(nameof(_httpContext)); + throw new ArgumentNullException(nameof(_httpContextAccessor.HttpContext)); } if(key == null) @@ -186,12 +184,12 @@ private void Set(string key, string value, CookieOptions option, int? expireTime { string protecetedData = _dataProtector.Protect(value); var encodedValue = Base64TextEncoder.Encode(protecetedData); - _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, encodedValue, option); + _chunkingHttpCookie.AppendResponseCookie(_httpContextAccessor.HttpContext, key, encodedValue, option); } else { //just append the cookie - _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, value, option); + _chunkingHttpCookie.AppendResponseCookie(_httpContextAccessor.HttpContext, key, value, option); } } diff --git a/src/CookieManager/ICookieManager.cs b/src/CookieManager/ICookieManager.cs index ef2a9d9..600c766 100644 --- a/src/CookieManager/ICookieManager.cs +++ b/src/CookieManager/ICookieManager.cs @@ -1,8 +1,5 @@ using Microsoft.AspNetCore.Http; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace CookieManager { diff --git a/src/CookieManager/docs/README.md b/src/CookieManager/docs/README.md new file mode 100644 index 0000000..0190b05 --- /dev/null +++ b/src/CookieManager/docs/README.md @@ -0,0 +1,74 @@ +## Cookie Manager Usages + +### ICookieManager interface + +```csharp +public class MyCookie +{ + public string Id { get; set; } + + public DateTime Date { get; set; } + + public string Indentifier { get; set; } +} + +// Get the myCookie object +MyCookie objFromCookie = _cookieManager.Get("Key"); + +// Set the myCookie object +MyCookie cooObj= new MyCookie() +{ + Id = Guid.NewGuid().ToString(), + Indentifier = "valueasgrsdgdf66514sdfgsd51d65s31g5dsg1rs5dg", + Date = DateTime.Now +}; +_cookieManager.Set("Key", cooObj, 60); + +// Get or set +// CookieOption example +MyCookie myCook = _cookieManager.GetOrSet("Key", () => +{ + // Write function to store output in cookie + return new MyCookie() + { + Id = Guid.NewGuid().ToString(), + Indentifier = "valueasgrsdgdf66514sdfgsd51d65s31g5dsg1rs5dg", + Date = DateTime.Now + }; + +}, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(1) }); + +``` +### ICookie interface + +```csharp +// Gets a cookie item associated with key +_cookie.Get("Key"); + +// Sets the cookie +_cookie.Set("Key", "value here", new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(1) }); + +``` + +### Configure Option +Add CookieManager in startup class in Configure Service +```csharp +// Add CookieManager +services.AddCookieManager(); + +// or + +// Add CookieManager with options +services.AddCookieManager(options => +{ + // Allow cookie data to encrypt by default it allow encryption + options.AllowEncryption = false; + // Throw if not all chunks of a cookie are available on a request for re-assembly. + options.ThrowForPartialCookies = true; + // Set null if not allow to devide in chunks + options.ChunkSize = null; + // Default Cookie expire time if expire time set to null of cookie + // Default time is 1 day to expire cookie + options.DefaultExpireTimeInDays = 10; +}); +``` \ No newline at end of file