Cookies need to be deleted after a click on not agree or accept On the index page is only a session cookie and it is deleted after click. But after a refresh or redirect to another page, the message still appears and cookies are not deleted. How can I delete all cookies on the index page and message not appear another time? Here is my code in controller and view:
[AllowAnonymous]
public ActionResult ClearCookies()
{
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
string userID = System.Web.HttpContext.Current.User.Identity.GetUserId();
string sessionID = System.Web.HttpContext.Current.Session.SessionID;
for (int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
Response.Cookies.Remove("cookieName");
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
return View("Index");
}
[AllowAnonymous]
public ActionResult GetAllCookies()
{
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
Response.Cookies.Add(aCookie);
}
return View("Index");
}
And on View :
<script>
$("#notagree").click(function () {
$.ajax({
url: '@Url.Action("ClearAllCookies","Account")',
type: "post",
success: function (result) {
},
error: function (result) {
$("#ErrorModal").modal("show");
}
});
};
});
$("#agree").click(function () {
$.ajax({
url: '@Url.Action("GetAllCookies","Account")',
type: "post",
success: function (result) {
},
error: function (result) {
$("#ErrorModal").modal("show");
}
});
});
</script>