Cookie Manager Wrapper In ASP.NET Core

Introduction

In this article, you will learn how to work with cookies in an ASP.NET Core style (in the form of an interface), abstraction layer on top of cookie object and how to secure cookie data.

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 

  1. Strongly Typed - CookieManager interface allows you to play with generic object. You don't have to care about casting or serialization.
  2. Func<TResult> support - Encapsulates a method, which returns a value of the type specified by the TResult parameter.
  3. Secure - The cookie data is protected with the machine key, using security algorithm. For more about data protection click here.
  4. Configure Service Extension - Just add the CookieManager in Configure Service.
  5. Ease to use - The interfaces allows to ease use of http cookie. 
ICookieManager interface

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.

  1. /// <summary>  
  2.     /// Cookie Manager is abstraction layer on top of <see cref="ICookie" />  
  3.     /// </summary>  
  4.     public interface ICookieManager  
  5.     {     
  6.         /// <summary>  
  7.         /// Gets a cookie associated with key   
  8.         /// </summary>  
  9.         /// <typeparam name="T">TSource</typeparam>  
  10.         /// <param name="key">Key</param>  
  11.         /// <returns>TSource object</returns>  
  12.         T Get<T>(string key);  
  13.   
  14.         /// <summary>  
  15.         /// Gets the cookie object and set if the cookie is expired  
  16.         /// </summary>  
  17.         /// <typeparam name="T">TSource</typeparam>  
  18.         /// <param name="key">Key</param>  
  19.         /// <param name="acquirer">function</param>  
  20.         /// <param name="expireTime">cookie expire time</param>  
  21.         /// <returns></returns>  
  22.         T GetOrSet<T>(string key, Func<T> acquirer, int? expireTime = null);  
  23.   
  24.         /// <summary>  
  25.         /// Gets the cookie object and set if the cookie is expired 
  26.         /// </summary>  
  27.         /// <typeparam name="T">TSource</typeparam>  
  28.         /// <param name="key">Key</param>  
  29.         /// <param name="acquirer">function</param>  
  30.         /// <param name="option">cookie option</param>  
  31.         /// <returns></returns>  
  32.         T GetOrSet<T>(string key, Func<T> acquirer, CookieOptions option);  
  33.   
  34.         /// <summary>  
  35.         /// Sets the cookie value  
  36.         /// </summary>  
  37.         /// <param name="key">Key</param>  
  38.         /// <param name="value">value of the key</param>  
  39.         /// <param name="expireTime">cookie expire time</param>  
  40.         void Set(string key, object value, int? expireTime = null);  
  41.   
  42.         /// <summary>  
  43.         /// Sets the cookie value  
  44.         /// </summary>  
  45.         /// <param name="key">Key</param>  
  46.         /// <param name="value">value of the key</param>  
  47.         /// <param name="expireTime">cookie expire time</param>  
  48.         void Set(string key, object value, CookieOptions option);  
  49.   
  50.         /// <summary>  
  51.         /// Contains the key  
  52.         /// </summary>  
  53.         /// <param name="key">Key</param>  
  54.         /// <returns>bool</returns>  
  55.         bool Contains(string key);  
  56.   
  57.         /// <summary>  
  58.         /// remove the Key  
  59.         /// </summary>  
  60.         /// <param name="key"></param>  
  61.         void Remove(string key);  
  62.     }   
ICookie Interface

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.
  1. /// <summary>  
  2.     /// ICookie is absstraction layer on top of ASP.Net Core Cookie API  
  3.     /// </summary>  
  4.     public interface ICookie  
  5.     {  
  6.   
  7.         /// <summary>  
  8.         ///   
  9.         /// </summary>  
  10.         ICollection<string> Keys { get; }  
  11.   
  12.         /// <summary>  
  13.         /// Gets a cookie item associated with key  
  14.         /// </summary>  
  15.         /// <param name="key"></param>  
  16.         /// <returns></returns>  
  17.         string Get(string key);  
  18.   
  19.         /// <summary>  
  20.         /// Sets the cookie   
  21.         /// </summary>  
  22.         /// <param name="key">key</param>  
  23.         /// <param name="value">value of the key</param>  
  24.         /// <param name="expireTime">cookie expire time</param>  
  25.         void Set(string key, string value, int? expireTime);  
  26.   
  27.         /// <summary>  
  28.         /// Sets the cookie   
  29.         /// </summary>  
  30.         /// <param name="key">key</param>  
  31.         /// <param name="value">value of the key</param>  
  32.         /// <param name="expireTime">cookie expire time</param>  
  33.         void Set(string key, string value, CookieOptions option);  
  34.   
  35.         /// <summary>  
  36.         /// Contain the key  
  37.         /// </summary>  
  38.         /// <param name="key">Key</param>  
  39.         /// <returns>bool</returns>  
  40.         bool Contains(string key);  
  41.   
  42.         /// <summary>  
  43.         /// delete the key from cookie   
  44.         /// </summary>  
  45.         /// <param name="key"></param>  
  46.         void Remove(string key);  
  47.     }   

Usages of CookieManager

Add CookieManager in startup Configure Service
  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2.         public void ConfigureServices(IServiceCollection services)  
  3.         {  
  4.             // Add framework services.  
  5.             services.AddMvc();  
  6.   
  7.             //add CookieManager  
  8.             services.AddCookieManager();  
  9.         }  
Access the CookieManager API.
  1. //Get or set <T>  
  2.             //Cookieoption example  
  3.             MyCookie myCook =  _cookieManager.GetOrSet<MyCookie>("Key", () =>   
  4.             {  
  5.                 //write fucntion to store  output in cookie  
  6.                 return new MyCookie()  
  7.                 {  
  8.                         Id = Guid.NewGuid().ToString(),  
  9.                         Indentifier = "value here",  
  10.                         Date = DateTime.Now  
  11.                 };  
  12.   
  13.             },new CookieOptions() { HttpOnly = true,Expires = DateTime.Now.AddDays(1) });  
  14.   
  15.             // expire time example  
  16.             MyCookie myCookWithExpireTime = _cookieManager.GetOrSet<MyCookie>("Key", () =>  
  17.             {  
  18.                 //write fucntion to store  output in cookie  
  19.                 return new MyCookie()  
  20.                 {  
  21.                     Id = Guid.NewGuid().ToString(),  
  22.                     Indentifier = "value here",  
  23.                     Date = DateTime.Now  
  24.                 };  
  25.   
  26.             }, 60);   
Source code is available on Github - https://github.com/nemi-chand/CookieManager

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.

Up Next
    Ebook Download
    View all
    Learn
    View all