ASP.NET - How to remove(clear) the cookies - Asked By aileni giri on 07-May-12 07:29 AM

 i have problem with  coockies as  follows..
      in my master page i have a logout button ,when i press the logout button all the  cookies should be deleted.



..any body pls help me out....
Somesh Yadav replied to aileni giri on 07-May-12 07:33 AM

here is the code to create the cookie & delete it...

              //Create Cookie
              HttpCookie aCookie = new HttpCookie("AdminInfo");
              aCookie.Values["userName"] = txtUsername.Text;
              aCookie.Values["Password"] = txtPassword.Text;
              aCookie.Values["lastVisit"] = DateTime.Now.ToString();
              aCookie.Expires = DateTime.Now.AddDays(10);
              Response.Cookies.Add(aCookie);
         
              //Delete Cookie
              HttpCookie aCookie = new HttpCookie("AdminInfo");              
              aCookie.Expires = DateTime.Now.AddDays(-1);
              Response.Cookies.Add(aCookie);
          

Hope this will help u..
Venkat K replied to aileni giri on 07-May-12 07:59 AM
This is a javascript cookie to begin with,

function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name)
{
createCookie(name,"",-1);
}

All that need be done is invoke eraseCookie(name), with the proper cookie name, onload of the login page, an event on it or other page(s), or onunload of whatever page(s) or in association with the logout event.

Thanks
Asked By aileni giri on 07-May-12 08:00 AM
it is  working fine ... i am so thankfull to you
dipa ahuja replied to aileni giri on 07-May-12 03:54 PM
See this simple example
 
//Write username to the cookie
Response.Cookies["PBLOGIN"]["UNAME"] = this.TbUserName.Text;
 
//Write password to the cookie
Response.Cookies["PBLOGIN"]["UPASS"] = this.TbPassword.Text;
 
//Create a cookie with expiry of 30 days
Response.Cookies["PBLOGIN"].Expires = DateTime.Now.AddDays(30);
 
Now next time when you want to chek wheather the cookie exist check it this way and assign new value the cookie variables:
 
if ((Request.Cookies["PBLOGIN"] != null))
{
     Response.Cookies["PBLOGIN"]["UNAME"] = this.TbUserName.Text;
     Response.Cookies["PBLOGIN"]["UPASS"] = this.TbPassword.Text;
}