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. /// <summary>
  14. /// Gets the cookie object and set if the cookie is expired
  15. /// </summary>
  16. /// <typeparam name="T">TSource</typeparam>
  17. /// <param name="key">Key</param>
  18. /// <param name="acquirer">function</param>
  19. /// <param name="expireTime">cookie expire time</param>
  20. /// <returns></returns>
  21. T GetOrSet<T>(string key, Func<T> acquirer, int? expireTime = null);
  22. /// <summary>
  23. /// Gets the cookie object and set if the cookie is expired
  24. /// </summary>
  25. /// <typeparam name="T">TSource</typeparam>
  26. /// <param name="key">Key</param>
  27. /// <param name="acquirer">function</param>
  28. /// <param name="option">cookie option</param>
  29. /// <returns></returns>
  30. T GetOrSet<T>(string key, Func<T> acquirer, CookieOptions option);
  31. /// <summary>
  32. /// Sets the cookie value
  33. /// </summary>
  34. /// <param name="key">Key</param>
  35. /// <param name="value">value of the key</param>
  36. /// <param name="expireTime">cookie expire time</param>
  37. void Set(string key, object value, int? expireTime = null);
  38. /// <summary>
  39. /// Sets the cookie value
  40. /// </summary>
  41. /// <param name="key">Key</param>
  42. /// <param name="value">value of the key</param>
  43. /// <param name="expireTime">cookie expire time</param>
  44. void Set(string key, object value, CookieOptions option);
  45. /// <summary>
  46. /// Contains the key
  47. /// </summary>
  48. /// <param name="key">Key</param>
  49. /// <returns>bool</returns>
  50. bool Contains(string key);
  51. /// <summary>
  52. /// remove the Key
  53. /// </summary>
  54. /// <param name="key"></param>
  55. void Remove(string key);
  56. }
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. /// <summary>
  7. ///
  8. /// </summary>
  9. ICollection<string> Keys { get; }
  10. /// <summary>
  11. /// Gets a cookie item associated with key
  12. /// </summary>
  13. /// <param name="key"></param>
  14. /// <returns></returns>
  15. string Get(string key);
  16. /// <summary>
  17. /// Sets the cookie
  18. /// </summary>
  19. /// <param name="key">key</param>
  20. /// <param name="value">value of the key</param>
  21. /// <param name="expireTime">cookie expire time</param>
  22. void Set(string key, string value, int? expireTime);
  23. /// <summary>
  24. /// Sets the cookie
  25. /// </summary>
  26. /// <param name="key">key</param>
  27. /// <param name="value">value of the key</param>
  28. /// <param name="expireTime">cookie expire time</param>
  29. void Set(string key, string value, CookieOptions option);
  30. /// <summary>
  31. /// Contain the key
  32. /// </summary>
  33. /// <param name="key">Key</param>
  34. /// <returns>bool</returns>
  35. bool Contains(string key);
  36. /// <summary>
  37. /// delete the key from cookie
  38. /// </summary>
  39. /// <param name="key"></param>
  40. void Remove(string key);
  41. }

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. //add CookieManager
  7. services.AddCookieManager();
  8. }
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. },new CookieOptions() { HttpOnly = true,Expires = DateTime.Now.AddDays(1) });
  13. // expire time example
  14. MyCookie myCookWithExpireTime = _cookieManager.GetOrSet<MyCookie>("Key", () =>
  15. {
  16. //write fucntion to store output in cookie
  17. return new MyCookie()
  18. {
  19. Id = Guid.NewGuid().ToString(),
  20. Indentifier = "value here",
  21. Date = DateTime.Now
  22. };
  23. }, 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.