How to delete a cookie when clicking on exit in Asp.Net MVC3

I have an asp.net MVC3 application with two Admin and General.Cookies users saved for both users. The following describes how my cookies are stored.

 Cookie.setCookie(No,DOB,User); 

There is a cookie class with setCookie method

 public static void setCookie(int No,DateTime DOB,string User)
        {
            HttpCookie MyCookie= new HttpCookie("MyCookie");
            MyCookie["No"] = No;
            MyCookie["DOB"] = DOB;
            MyCookie["User"] = User;

            MyCookie.Expires.Add(new TimeSpan(0,30,0));

            HttpContext.Current.Response.Cookies.Add(MyCookie);
        }

Above is my Setcookie code.

MyCookie.Expires.Add(new TimeSpan(0,30,0));will give the default expiry time to 30 minutes.

What I try to do when the user clicks "Logout", the cookie must be completely cleared and the user will not be able to go to any of the authenticated sections of the site. I also tried this but not used

MyCookie.Expires.Add(-1);

But I tried this in my SetCookie method. How can I clear cookies when a user clicks the "Logout" button. I also tried to create an exit method to the Cookie class, as shown below:

  public static bool logout()
        {

            HttpCookie reqCookies = HttpContext.Current.Request.Cookies["MyCookie"];
            reqCookies.Expires.AddDays(-1);
            return true;
        }

im cookie Expire it, .AddDays(-1); if cookie.Logout()==true . RunTime DateTime. :

/ , .

+5
2

jquery cookie script.

<a href="" onclick="removeCookie();">logout button</a>

function removeCookie(){
$.cookie('cookiename', '');
}
+1

cookie ASP.NET, cookie .

+8

All Articles