Nemi Chand
Nemi Chand Nemi Chand is a C-sharp Corner MVP, Microsoft® Certified Professional,author,Speaker, senior developer and community lead. Nemi is passionate about community and all things .NET related, having worked with ASP.NET for over 7 years. Nemi is currently developing cloud-native services, using .NET Core, ASP.NET Core and Docker. He enjoys sharing his knowledge through his blog and by presenting at user groups. Nemi is excited to be a part of the .NET community. He enjoys contributing to and maintaining OSS projects, most actively helping save lives with open source software. You can find Nemi online at his blog www.nemi-chand.com and on Twitter as @nemidotnet

Working With Cookies in ASP.NET Core

Working With Cookies in ASP.NET Core

This article explains how ASP.NET Core deals with cookies. Cookies are key-value pair collections where we can read, write and delete using key. HTTP Cookie is some piece of data which is stored in the user’s browser.

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.

I have written a wrapper on top of Http Cookie which helps you to ease of use and secure the cookie data. CookieManager is an ASPNET Core Abstraction layer on top of cookie. Read more here .

HttpCookie is accessible from Request.Cookies. Given below is the sample code.

1
2
3
4
5
6
7
//read cookie from IHttpContextAccessor  
string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];  

//read cookie from Request object  
string cookieValueFromReq = Request.Cookies["Key"];

Here is the code snippet to write cookies. Set method to write cookies. CookieOption is available to extend the cookie behavior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/// <summary>  
/// set the cookie  
/// </summary>  
/// <param name="key">key (unique indentifier)</param>  
/// <param name="value">value to store in cookie object</param>  
/// <param name="expireTime">expiration time</param>  
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);  
}

Delete the cookie by key name.

1
2
3
4
5
6
7
8
/// <summary>  
/// Delete the key  
/// </summary>  
/// <param name="key">Key</param>  
public void Remove(string key)  
{  
      Response.Cookies.Delete(key);  
}

CookieOptions

It extends the cookie behavior in the browser.

Options

  1. Domain - The domain you want to associate with cookie
  2. Path - Cookie Path
  3. Expires - The expiration date and time of the cookie
  4. HttpOnly - Gets or sets a value that indicates whether a cookie is accessible by client-side script or not.
  5. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class HomeController : Controller  
{  
    private readonly IHttpContextAccessor _httpContextAccessor;  
  
    public HomeController(IHttpContextAccessor httpContextAccessor)  
    {  
        this._httpContextAccessor = httpContextAccessor;  
    }  
    public IActionResult Index()  
    {  
        //read cookie from IHttpContextAccessor  
        string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];  
        //read cookie from Request object  
        string cookieValueFromReq = Request.Cookies["Key"];  
        //set the key value in Cookie  
        Set("kay", "Hello from cookie", 10);  
        //Delete the cookie object  
        Remove("Key");  
        return View();  
    }  
    /// <summary>  
    /// Get the cookie  
    /// </summary>  
    /// <param name="key">Key </param>  
    /// <returns>string value</returns>  
    public string Get(string key)  
    {  
        return Request.Cookies[Key];  
    }  
    /// <summary>  
    /// set the cookie  
    /// </summary>  
    /// <param name="key">key (unique indentifier)</param>  
    /// <param name="value">value to store in cookie object</param>  
    /// <param name="expireTime">expiration time</param>  
    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);  
    }  
    /// <summary>  
    /// Delete the key  
    /// </summary>  
    /// <param name="key">Key</param>  
    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.

References

comments powered by Disqus