Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

How do you clear cookies using asp.net mvc 3 and c#?

Ok, so I really think I am doing this right, but the cookies aren't being cleared.

 Session.Clear();
 HttpCookie c = Request.Cookies["MyCookie"];
 if (c != null)
 {
     c = new HttpCookie("MyCookie");
     c["AT"] = null;
     c.Expires = DateTime.Now.AddDays(-1);
     Request.Cookies.Add(c);
 }

 return RedirectToAction("Index", "Home");

When the redirect happens, it finds the cookie again and moves on as though I never logged out. Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...