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);
- }

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?