none
Cookies problem RRS feed

  • Question

  • I click Button1 then Button2 then Button1 again and the cookie "lastvisit" is not removed. Do you know why? Thanks.

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    using System.Collections.Generic;

    namespace WebApplicationX
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    if (Request.Cookies["userName"] == null)
                    {

                        Response.Cookies["userName"].Value = "patrick";
                        Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
                    }

                    if (Request.Cookies["lastVisit"] == null)
                    {


                        HttpCookie aCookie = new HttpCookie("lastVisit");
                        aCookie.Value = DateTime.Now.ToString();
                        aCookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(aCookie);
                    }
                }
            }

            protected void Button1_Click(object sender, EventArgs e)
            {

                if (Request.Cookies["userName"] != null)
                {
                    HttpCookie aCookie = Request.Cookies["userName"];
                    Label1.Text = Server.HtmlEncode(aCookie.Value);
                }
                else Label1.Text = "";

                if (Request.Cookies["lastvisit"] != null)
                {
                    HttpCookie aCookie2 = Request.Cookies["lastvisit"];
                    Label2.Text = Server.HtmlEncode(aCookie2.Value);
                }
                else Label2.Text = "";

            }
           

            protected void Button2_Click(object sender, EventArgs e)
            {
                Response.Cookies["userName"].Expires = DateTime.Now.AddDays(-1);

                HttpCookie aCookie2 = Request.Cookies["lastvisit"];
                aCookie2.Expires = DateTime.Now.AddDays(-1);

            }


    • Edited by DLewicki Thursday, December 8, 2011 2:19 PM
    Thursday, December 8, 2011 2:18 PM

Answers

  • Sorry but I misled you a little.  For ASP.NET the response cookies aren't populated automatically from the request cookies.  The request cookies come from the browser and the response cookies are sent from the server.  When the browser receives a response with cookies it merges the client-side versions with the new ones.

    Therefore to remove a cookie from the client you have to first see if it exists on the client via the Request.Cookies object.  If it exists you can update the expiration but then you have to add it to the list of cookies the response will return via Response.Cookies.  Then the server will send the updated cookie to the client and the client will merge them together.  The result is that the client will remove the cookie.  But note that the cookie is still going to be available during the current server request (it did come across from the client) but the next postback will have it removed.

    Here's a simple helper method

    private void RemoveCookie ( string name )
    {
        var cookie = Request.Cookies[name];
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        };
    }
    

    Michael Taylor - 12/8/2011
    http://msmvps.com/blogs/p3net

    Thursday, December 8, 2011 9:15 PM
    Moderator

All replies

  • Cookies have no relation to postbacks.  A cookie, once set, remains until either it expires or you explicitly remove it.  You're doing neither in your code.  To remove a cookie use the Remove method of the Cookies property.  You should also refer to the MSDN documentation about some points related to setting expiration - http://msdn.microsoft.com/en-us/library/system.web.httpcookie.expires.aspx

    Finally please note that the C# General forum is for UI-agnostic questions.  Please refer to the sticky post at the top of these forums for the correct forum for questions related to UI stuff like ASP.NET.  Please post future ASP.NET questions in the correct forum.  Thanks.

    Michael Taylor - 12/8/2011
    http://msmvps.com/blogs/p3net

    Thursday, December 8, 2011 2:47 PM
    Moderator
  •  

               aCookie2.Expires = DateTime.Now.AddDays(-1):

     

    Aren't I effectively removing the cookie with the above line of code found in Button2_click?

    I don't see any Remove method of the cookies property.


    • Edited by DLewicki Thursday, December 8, 2011 5:57 PM
    Thursday, December 8, 2011 5:48 PM
  • Not really.  Remove it properly using the Remove method and see if your problem goes away.

    Michael Taylor - 12/

    Thursday, December 8, 2011 5:53 PM
    Moderator
  • I can't do aCookie2.Remove();  Intellisense doesn't let me do that.
    Thursday, December 8, 2011 7:02 PM
  • Remove is on the cookie collection, not the cookie itself.  For the response you'd be using the Response.Cookies object.  When the response is built and sent back to the client then the cookies in that collection are returned.  The Request.Cookies is for getting the cookies that were sent in the last request.

    Michael Taylor - 12/8/2011
    http://msmvps.com/blogs/p3net

    Thursday, December 8, 2011 7:34 PM
    Moderator
  • I tried the following two lines of code and they did not remove the cookies:

     

                Response.Cookies.Remove("userName");

                Response.Cookies.Remove("lastvisit");

    Thursday, December 8, 2011 8:44 PM
  • Sorry but I misled you a little.  For ASP.NET the response cookies aren't populated automatically from the request cookies.  The request cookies come from the browser and the response cookies are sent from the server.  When the browser receives a response with cookies it merges the client-side versions with the new ones.

    Therefore to remove a cookie from the client you have to first see if it exists on the client via the Request.Cookies object.  If it exists you can update the expiration but then you have to add it to the list of cookies the response will return via Response.Cookies.  Then the server will send the updated cookie to the client and the client will merge them together.  The result is that the client will remove the cookie.  But note that the cookie is still going to be available during the current server request (it did come across from the client) but the next postback will have it removed.

    Here's a simple helper method

    private void RemoveCookie ( string name )
    {
        var cookie = Request.Cookies[name];
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        };
    }
    

    Michael Taylor - 12/8/2011
    http://msmvps.com/blogs/p3net

    Thursday, December 8, 2011 9:15 PM
    Moderator