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
- 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.
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.
-
-
-
- public interface ICookieManager
- {
-
-
-
-
-
-
- T Get<T>(string key);
-
-
-
-
-
-
-
-
-
- T GetOrSet<T>(string key, Func<T> acquirer, int? expireTime = null);
-
-
-
-
-
-
-
-
-
- T GetOrSet<T>(string key, Func<T> acquirer, CookieOptions option);
-
-
-
-
-
-
-
- void Set(string key, object value, int? expireTime = null);
-
-
-
-
-
-
-
- void Set(string key, object value, CookieOptions option);
-
-
-
-
-
-
- bool Contains(string key);
-
-
-
-
-
- void Remove(string key);
- }
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.
-
-
-
- public interface ICookie
- {
-
-
-
-
- ICollection<string> Keys { get; }
-
-
-
-
-
-
- string Get(string key);
-
-
-
-
-
-
-
- void Set(string key, string value, int? expireTime);
-
-
-
-
-
-
-
- void Set(string key, string value, CookieOptions option);
-
-
-
-
-
-
- bool Contains(string key);
-
-
-
-
-
- void Remove(string key);
- }
Usages of CookieManager
Add CookieManager in startup Configure Service
-
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddMvc();
-
-
- services.AddCookieManager();
- }
Access the CookieManager API.
-
-
- MyCookie myCook = _cookieManager.GetOrSet<MyCookie>("Key", () =>
- {
-
- return new MyCookie()
- {
- Id = Guid.NewGuid().ToString(),
- Indentifier = "value here",
- Date = DateTime.Now
- };
-
- },new CookieOptions() { HttpOnly = true,Expires = DateTime.Now.AddDays(1) });
-
-
- MyCookie myCookWithExpireTime = _cookieManager.GetOrSet<MyCookie>("Key", () =>
- {
-
- 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.