Read and Write Cookies in ASP.NET Core 1.0

Read and Write Cookies in ASP.NET Core 1.0

One of the basic requirements in several web applications is storing and retrieving small pieces of information in cookies. This article will discuss with an instance how ASP.NET Core 1.0 deals with cookies. You will learn here to read and write cookies using ASP.NET Core. You will also know how to configure the cookie properties such as expiration time.

You need to specify some preferences like font name, colour and font size. You can choose a preference from the dropdown list and mention its value in the textbox. Click on the Write Cookie button saves that choice in a cookie. The Is Persistent checkbox controls if the cookies are to be stored on the local disk such that they are read during a different browser session. The read cookies link takes you to another page where the choices are read and applied to a some HTML markup.

Begin by developing a new ASP.NET Core Web Application using VS 2015. Next open the HomeController and add 3 actions to it – Index(), WriteCookies() and ReadCookies().

The Index() action is quite straight and simply returns the Index view to the browser.

public IActionResult Index()


return View();


The WriteCookies() action writes the cookies and is shown below:

public IActionResult WriteCookies(string setting,

string settingValue,bool isPersistent)


if (isPersistent)


CookieOptions options = new CookieOptions();

options.Expires = DateTime.Now.AddDays(1);

Response.Cookies.Append(setting, settingValue,options);




Response.Cookies.Append(setting, settingValue);


ViewBag.Message = “Cookie written successfully!”;

return View(“Index”);


The WriteCookies() method receives 3 parameters viz. setting, settingValue and isPersistent through model binding. The set parameter will be the value selected from the dropdown list. The setting Value parameter will be the value entered in the textbox and isPersistent will point whether isPersistent checkbox is seen or not.

Inside, the code checks the value of isPersistent boolean parameter. If it is correct an object of CookieOptions class (Microsoft.AspNetCore.Http namespace) is created. The CookieOptions class helps you to specify the properties of the cookie as mentioned below:

  • Domain (associated with cookie)
  • Expires (when cookie must expire)
  • Path (specifies path where cookie is applicable)
  • Secure (cookie is sent on https channel only)
  • HttpOnly (client side script can’t reach the cookie)

When you set the Expires property of the cookie to one day from now; the cookie will persist on the client machine for a day. Then a cookie is written to the response using the Append() method of the Cookies collection. The Append() method accepts key, value and Cookie Options object.

The else block of the code simply adds a cookie by setting its key and value.

A ViewBag message points to the user that the choice is stored successfully.

Then append Index view and write the following into it:

<h1>Specify your preferences :</h1>

<form asp-action=”WriteCookies” asp-controller=”Home”>

<select name=”setting”>

<option value=”fontName”>Font Name</option>

<option value=”fontSize”>Font Size</option>

<option value=”color”>Color</option>


<input type=”text” name=”settingValue” />

<input type=”checkbox” name=”isPersistent”

value=”true” /> Is Persistent?

<input type=”submit” value=”Write Cookie” />




<a asp-action=”ReadCookies” asp-controller=”Home”>

Read Cookies



The Index view renders a <form> shown previous using ASP.NET Core tag helpers and inputs. It also outputs the Message property from ViewBag. A Read Cookies link will take the user to a test page where choices are applied.

Now, append ReadCookies() action to the HomeController and write the below code to it :

public IActionResult ReadCookies()


ViewBag.FontName = Request.Cookies[“fontName”];

ViewBag.FontSize = Request.Cookies[“fontSize”];

ViewBag.Color = Request.Cookies[“color”];



ViewBag.FontName = “Arial”;


if (string.IsNullOrEmpty(ViewBag.FontSize))


ViewBag.FontSize = “22px”;


if (string.IsNullOrEmpty(ViewBag.Color))


ViewBag.Color = “Blue”;


return View();


The ReadCookies() action reads 3cookies with the help of their keys. This is achieved using Cookies collection of the Request object. The cookie values are stored in ViewBag properties. A series of if statements check whether a particular preference is empty and if so assign a default value to it.

The ReadCookies view where these preferences are used is shown below:

<div style=”font-family:’@ViewBag.FontName’;



Hello World!


The ReadCookies view consists of a <div> element whose style feature makes use of font name, font size and colour specified in the respective ViewBag properties.

This concludes the application. Run it and specify values to the three choices. Don’t check the Is Persistent checkbox during saving the values. Then click on the Read Cookies link.

We conclude now….. Enjoy coding!

Let us know your opinion in the comments sections below. And feel free to refer Microsoft’s site to gather more information.

If you want to improve your skill in ASP.Net and excel yourself in ASP.NET training program; our institute, CRB Tech Solutions would be of great help and for you. Come and join us with our well structured program for ASP .Net.

Stay connected to CRB Tech for more technical optimization and other updates and information