How to Clear Cookies Using ASP.NET MVC 3 and C#

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



Leave a reply



Submit