Article

Delete a Cookie in ASP.Net MVC

In this article we will learn , how to Delete a Cookie in ASP.Net MVC.

Cookies cannot be cleared, it can be made to expire by setting its Expiry Date to a Past Date in ASP.Net MVC.

Home Controller

The Home Controller have two Action methods.

Action method for handling GET operation. Inside this Action method, first a check is performed whether Cookie exists and if the Cookie does not exists, it creates a Cookie using an object of the HttpCookie class and the Cookie is added to the Response.Cookies collection.

DeleteCookie Action Method

When we clicked on Delete Cookie Button, the DeleteCookie Action method is performed and where first a check is performed whether the Cookie exists in the Browser.

Then the Cookie object is fetched from the Request.Cookies collection using its unique Key.

A Cookie cannot be remove or delet from Browser, it only can be made expired and hence the Expiry Date of the Cookie is set to a past date and the Cookie is updated back into the Response.Cookies collection.

Key Points :


//Create a Cookie with a unique Key.
var nameOfCookie = new HttpCookie(“FirstName”)
//Set the Cookies value.
nameOfCookie.Values[“FirstName”] = “Laxmikant Upadhyay”
//Set the Expiry date.
nameOfCookie.Expires = DateTime.Now.AddDays(30)
//Add the Cookie to Browser.
Response.Cookies.Add(nameOfCookie)
//Redirect in order to save Cookie in Browser.
return RedirectToAction(“Index”)
/strong>

Write this code on HomeController.cs page

[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DemoMVCAPP.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
if (TempData["Message"] == null && Request.Cookies["FirstName"] == null)
{

var nameOfCookie = new HttpCookie("FirstName");

nameOfCookie.Values["FirstName"] = "Laxmikant Upadhyay";

nameOfCookie.Expires = DateTime.Now.AddDays(30);

Response.Cookies.Add(nameOfCookie);

return RedirectToAction("Index");
}
return View();
}

[HttpPost]
public ActionResult DeleteCookie()
{
//Check if Cookie exists.
if (Request.Cookies["FirstName"] != null)
{

var nameCookie = Request.Cookies["FirstName"];
nameCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(nameCookie);
TempData["Message"] = "Cookie deleted.";
}
else
{
//Set Message in TempData.
TempData["Message"] = "Cookie not found.";
}

return RedirectToAction("Index");
}

}
}
[/php]

Index.cshtml:

Key point :
ActionName – Name of the Action.

ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists a Submit Button for deleting a Cookie.
Above the Submit Button, the value of the Cookie is displayed using Razor Syntax.
When the Delete Cookie button is clicked, the value from the TempData object is displayed using JavaScript Alert Message Box.

[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DemoMVCAPP.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
if (TempData["Message"] == null && Request.Cookies["FirstName"] == null)
{

var nameOfCookie = new HttpCookie("FirstName");

nameOfCookie.Values["FirstName"] = "Laxmikant Upadhyay";

nameOfCookie.Expires = DateTime.Now.AddDays(30);

Response.Cookies.Add(nameOfCookie);

return RedirectToAction("Index");
}
return View();
}

[HttpPost]
public ActionResult DeleteCookie()
{
//Check if Cookie exists.
if (Request.Cookies["FirstName"] != null)
{
//Fetch the Cookie using its Key.
var nameCookie = Request.Cookies["FirstName"];

//Set the Expiry date to past date.
nameCookie.Expires = DateTime.Now.AddDays(-1);

//Update the Cookie in Browser.
Response.Cookies.Add(nameCookie);

//Set Message in TempData.
TempData["Message"] = "Cookie deleted.";
}
else
{
//Set Message in TempData.
TempData["Message"] = "Cookie not found.";
}

return RedirectToAction("Index");
}

}
}
[/php]

Output

delete-a-cookie-in-asp-net-mvc

delete-a-cookie-in-asp-net-mvc

Leave a Reply

Your email address will not be published.