Introduction
HTTP Cookie is some piece of data which is stored in the user's browser. HTTP cookies play a vital role in the software world. We can store users' related information in cookies and there are many other usages. In asp.net core working with cookies is made easy. I've written a couple of abstraction layers on top of Http cookie object. Cookies are key-value pair collections where we can read, write and delete using key.
In ASP.NET, we can access cookies using httpcontext.current but in ASP.NET Core, there is no htttpcontext.currently. In ASP.NET Core, everything is decoupled and modular.
Httpcontext is accessible from Request object and the IHttpContextAccessor interface which is under "Microsoft.AspNetCore.Http" namespace and this is available anywhere in the application.
Reading Cookie
HttpCookie is accessible from Request.Cookies. Given below is the sample code,
-
- string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];
-
- string cookieValueFromReq = Request.Cookies["Key"];
Writing cookie
Here is the code snippet to write cookies. Set method to write cookies. CookieOption is available to extend the cookie behavior.
-
-
-
-
-
-
- public void Set(string key, string value, int? expireTime)
- {
- CookieOptions option = new CookieOptions();
- if (expireTime.HasValue)
- option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
- else
- option.Expires = DateTime.Now.AddMilliseconds(10);
-
- Response.Cookies.Append(key, value, option);
- }
Remove Cookie
Delete the cookie by key name.
-
-
-
-
- public void Remove(string key)
- {
- Response.Cookies.Delete(key);
- }
CookieOptions
It extends the cookie behavior in the browser.
Options
- Domain - The domain you want to associate with cookie
- Path - Cookie Path
- Expires - The expiration date and time of the cookie
- HttpOnly - Gets or sets a value that indicates whether a cookie is accessible by client-side script or not.
- Secure - Transmit the cookie using Secure Sockets Layer (SSL) that is, over HTTPS only.
Here is the complete code example to read, write and delete the cookie.
- public class HomeController : Controller
- {
- private readonly IHttpContextAccessor _httpContextAccessor;
-
- public HomeController(IHttpContextAccessor httpContextAccessor)
- {
- this._httpContextAccessor = httpContextAccessor;
- }
- public IActionResult Index()
- {
-
- string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];
-
- string cookieValueFromReq = Request.Cookies["Key"];
-
- Set("kay", "Hello from cookie", 10);
-
- Remove("Key");
- return View();
- }
-
-
-
-
-
- public string Get(string key)
- {
- return Request.Cookies["Key"];
- }
-
-
-
-
-
-
- public void Set(string key, string value, int? expireTime)
- {
- CookieOptions option = new CookieOptions();
- if (expireTime.HasValue)
- option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
- else
- option.Expires = DateTime.Now.AddMilliseconds(10);
- Response.Cookies.Append(key, value, option);
- }
-
-
-
-
- public void Remove(string key)
- {
- Response.Cookies.Delete(key);
- }
- }
I hope you learned how to work with cookies in ASP.NET Core. I have shown you an example of reading ,writing and removing cookie objects.