简体   繁体   中英

How to clear cookie in asp.net mvc?

I have tried the following but, the cookie is not expired after the given time. Can you please look into my code

System.Web.HttpCookie currentUserCookie = System.Web.HttpContext.Current.Request.Cookies["Userdata"];
System.Web.HttpContext.Current.Response.Cookies.Remove("Userdata");
currentUserCookie.Expires = DateTime.Now.AddMinutes(10);

Can anyone help me?

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: https://msdn.microsoft.com/en-us/library/ms178195.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM