How do you clear cookies using asp.net mvc 3 and c#?
You're close. You'll need to use the Response object to write back to the browser:
if ( Request.Cookies["MyCookie"] != null )
{
var c = new HttpCookie( "MyCookie" );
c.Expires = DateTime.Now.AddDays( -1 );
Response.Cookies.Add( c );
}
More information on MSDN, How to: Delete a Cookie.
How do I manually delete a cookie in asp.net MVC 4
Try:
if (Request.Cookies["MyCookie"] != null)
{
var c = new HttpCookie("MyCookie")
{
Expires = DateTime.Now.AddDays(-1)
};
Response.Cookies.Add(c);
}
More information on MSDN.
How to remove all current domain cookies in MVC website?
How about this?
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
How to delete cookies on an ASP.NET website
Try something like that:
if (Request.Cookies["userId"] != null)
{
Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);
}
But it also makes sense to use
Session.Abandon();
besides in many scenarios.
Delete Cookie in Multiple Action Request in ASP.NET MVC
Cookies are stored client-side, so the cookie isn't actually deleted until the client gets the response header directing it to. Then, on the next request, it will no longer send that cookie, but you cannot delete and check that it's been deleted in the same request, since it actually has not been deleted yet. Generally, when you delete a cookie, you want to return a redirect, even if it's to the same URL. That way, the client will be forced to make a new cookie-less request.
Clients need to delete cookies most of the times in MVC
Thanks to all of you for having tried to reply to the question. Indeed they were all helpful. I solved the problem myself by adding a machinekey to the wenconfig, as well as a form name had to be there. without te form name, even the machine key was not useful
Related Topics
How to Concatenate Two Ienumerable<T> into a New Ienumerable<T>
How to 'Await' Raising an Eventhandler Event
Why Folderbrowserdialog Dialog Does Not Scroll to Selected Folder
C# Working with Entity Framework in a Multi Threaded Server
Why Use a Using Statement with a SQLtransaction
Why Method Overloading Is Not Allowed in Wcf
The Source File Is Different from When the Module Was Built
MVC 4 Edit Modal Form Using Bootstrap
The State of Linkers for .Net Apps (Aka "Please Sir, May I Have a Linker" 2009 Edition)
How to Write Output from a Unit Test
Cannot Load Counter Name Data Because an Invalid Index -Exception
Modelstate.Isvalid Even When It Should Not Be
How to Modify Existing Xml File with Xmldocument and Xmlnode in C#
How to Execute a Java Program from C#
Performance of "Direct" Virtual Call VS. Interface Call in C#
When to Use a Sortedlist<Tkey, Tvalue> Over a Sorteddictionary<Tkey, Tvalue>
Check If All Items Are the Same in a List
Differencebetween Streamwriter.Flush() and Streamwriter.Close()