Solved Logging out - clearing cookies

Posted on 2014-08-28
Last Modified: 2014-08-28

I am using the following code to keep a user logged in on my site
What code would I use to log them out. I presume that setting DateTime.Now.AddDays(-1)
for all of them would do the trick. Is this the best way

            If chkRememberMe.Checked Then
                Response.Cookies("UserName").Expires = DateTime.Now.AddDays(30)
                Response.Cookies("Password").Expires = DateTime.Now.AddDays(30)
                Response.Cookies("CompanyIDs").Expires = DateTime.Now.AddDays(30)
                Response.Cookies("UserName").Expires = DateTime.Now.AddDays(-1)
                Response.Cookies("Password").Expires = DateTime.Now.AddDays(-1)
                Response.Cookies("CompanyIDs").Expires = DateTime.Now.AddDays(-1)
            End If

            Response.Cookies("UserName").Value = txtUserName.Text.Trim
            Response.Cookies("Password").Value = txtPassword.Text.Trim
            Response.Cookies("CompanyIDs").Value = Me.lblCompanyIDs.Text
Question by:murbro
    LVL 20

    Accepted Solution

    Hi murbro,

    if (Request.Cookies["UserName"] != null)
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);  

    also use
            Response.Redirect("~/Login.aspx", false);

    Author Closing Comment

    Thanks very much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now