Introduction
For more about working with cookie click here.
Source code available on Github - https://github.com/nemi-chand/CookieManager
CookieManager is a .NET Core library to extend the cookie object and secure the data, which is encryped by the machine key, using "IDataProtector" dataprotector.
Features
- Strongly Typed - CookieManager interface allows you to play with generic object. You don't have to care about casting or serialization.
- Func<TResult> support - Encapsulates a method, which returns a value of the type specified by the TResult parameter.
- Secure - The cookie data is protected with the machine key, using security algorithm. For more about data protection click here.
- Configure Service Extension - Just add the CookieManager in Configure Service.
- Ease to use - The interfaces allows to ease use of http cookie.
Cookie Manager is an abstraction layer on top of ICookie Interface . This extends the Cookie behavior in terms of <TSource> generic support, Func<TResult>.This is implemented by DefaultCookieManager class . ICookie Interface is a depedenacy of this class. The list of APIs are given below in this interface.
- /// <summary>
- /// Cookie Manager is abstraction layer on top of <see cref="ICookie" />
- /// </summary>
- public interface ICookieManager
- {
- /// <summary>
- /// Gets a cookie associated with key
- /// </summary>
- /// <typeparam name="T">TSource</typeparam>
- /// <param name="key">Key</param>
- /// <returns>TSource object</returns>
- T Get<T>(string key);
- /// <summary>
- /// Gets the cookie object and set if the cookie is expired
- /// </summary>
- /// <typeparam name="T">TSource</typeparam>
- /// <param name="key">Key</param>
- /// <param name="acquirer">function</param>
- /// <param name="expireTime">cookie expire time</param>
- /// <returns></returns>
- T GetOrSet<T>(string key, Func<T> acquirer, int? expireTime = null);
- /// <summary>
- /// Gets the cookie object and set if the cookie is expired
- /// </summary>
- /// <typeparam name="T">TSource</typeparam>
- /// <param name="key">Key</param>
- /// <param name="acquirer">function</param>
- /// <param name="option">cookie option</param>
- /// <returns></returns>
- T GetOrSet<T>(string key, Func<T> acquirer, CookieOptions option);
- /// <summary>
- /// Sets the cookie value
- /// </summary>
- /// <param name="key">Key</param>
- /// <param name="value">value of the key</param>
- /// <param name="expireTime">cookie expire time</param>
- void Set(string key, object value, int? expireTime = null);
- /// <summary>
- /// Sets the cookie value
- /// </summary>
- /// <param name="key">Key</param>
- /// <param name="value">value of the key</param>
- /// <param name="expireTime">cookie expire time</param>
- void Set(string key, object value, CookieOptions option);
- /// <summary>
- /// Contains the key
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>bool</returns>
- bool Contains(string key);
- /// <summary>
- /// remove the Key
- /// </summary>
- /// <param name="key"></param>
- void Remove(string key);
- }
ICookie Interface is an abstraction layer on top of http cookie object, which secures the data. This is implemented by HttpCookie class. The list of APIs available in this interface are given below.
- /// <summary>
- /// ICookie is absstraction layer on top of ASP.Net Core Cookie API
- /// </summary>
- public interface ICookie
- {
- /// <summary>
- ///
- /// </summary>
- ICollection<string> Keys { get; }
- /// <summary>
- /// Gets a cookie item associated with key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- string Get(string key);
- /// <summary>
- /// Sets the cookie
- /// </summary>
- /// <param name="key">key</param>
- /// <param name="value">value of the key</param>
- /// <param name="expireTime">cookie expire time</param>
- void Set(string key, string value, int? expireTime);
- /// <summary>
- /// Sets the cookie
- /// </summary>
- /// <param name="key">key</param>
- /// <param name="value">value of the key</param>
- /// <param name="expireTime">cookie expire time</param>
- void Set(string key, string value, CookieOptions option);
- /// <summary>
- /// Contain the key
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>bool</returns>
- bool Contains(string key);
- /// <summary>
- /// delete the key from cookie
- /// </summary>
- /// <param name="key"></param>
- void Remove(string key);
- }
Usages of CookieManager
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddMvc();
- //add CookieManager
- services.AddCookieManager();
- }
- //Get or set <T>
- //Cookieoption example
- MyCookie myCook = _cookieManager.GetOrSet<MyCookie>("Key", () =>
- {
- //write fucntion to store output in cookie
- return new MyCookie()
- {
- Id = Guid.NewGuid().ToString(),
- Indentifier = "value here",
- Date = DateTime.Now
- };
- },new CookieOptions() { HttpOnly = true,Expires = DateTime.Now.AddDays(1) });
- // expire time example
- MyCookie myCookWithExpireTime = _cookieManager.GetOrSet<MyCookie>("Key", () =>
- {
- //write fucntion to store output in cookie
- return new MyCookie()
- {
- Id = Guid.NewGuid().ToString(),
- Indentifier = "value here",
- Date = DateTime.Now
- };
- }, 60);
I hope, this wrapper helps you to play around with httpcookie in an ASP.NET Core. The source code is available for download at GitHub.

Micha JanssenPosted Apr 10, 2019, 12:07 PM
Hello there! I have a question; where in gods name do you instantiate _cookieManager.. Could you please explain further?