locked
Webforms MasterPages & Cookie issues RRS feed

  • Question

  • User-195907812 posted

    Hi,

    I'm trying to remove a cookie in a page's code behind as follows:

                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies["WWW-Username"];
                    cookie.Expires = DateTime.UtcNow.AddYears(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    HttpContext.Current.Request.Cookies.Remove("WWW-Username");
                }
    			
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                }			

    Note that the second condition above is never met.

    I then redirect the user to the homepage, where the master page's Page_Init event is called. However, once here the cookie still exists.

                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp2 = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                }	

    Note this condition IS met.

    Am I missing something? I've checked my code and the cookie isn't re-created.

    Many thanks

    Thursday, August 20, 2020 9:01 PM

Answers

  • User-939850651 posted

    Hi RageRiot,

    According to your description, I created a simple example using the code you provided, and found that the problem you mentioned does exist.

    I modified part of the code to fix this problem:

    TestPage.aspx
    
    <asp:Button runat="server" ID="btn" Text="GO TO HOME" OnClick="btn_Click" />
    
    TestPage.aspx.cs
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack) {
                    HttpCookie cookie = new HttpCookie("WWW-Username");
                    cookie.Value = "TestVaue";
                    Response.Cookies.Set(cookie);
                }
            }
    
            protected void btn_Click(object sender, EventArgs e)
            {
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies["WWW-Username"];
                    cookie = new HttpCookie("WWW-Username");
                    cookie.Expires = DateTime.UtcNow.AddYears(-1);
                    //HttpContext.Current.Response.Cookies.Add(cookie);
                    HttpContext.Current.Response.Cookies.Set(cookie);
                    HttpContext.Current.Request.Cookies.Remove("WWW-Username");
                }
    
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                }
                Response.Redirect("home.aspx", false);
            }
    Site.Master.cs
    
    protected void Page_Load(object sender, EventArgs e) { }
    
    protected void Page_Init(object sender, EventArgs e)
            {
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp2 = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                    //Response.Write(tmp2);
                }
            }

    And an empty page home.aspx with no code behind.

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 3:42 AM

All replies

  • User-939850651 posted

    Hi RageRiot,

    According to your description, I created a simple example using the code you provided, and found that the problem you mentioned does exist.

    I modified part of the code to fix this problem:

    TestPage.aspx
    
    <asp:Button runat="server" ID="btn" Text="GO TO HOME" OnClick="btn_Click" />
    
    TestPage.aspx.cs
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack) {
                    HttpCookie cookie = new HttpCookie("WWW-Username");
                    cookie.Value = "TestVaue";
                    Response.Cookies.Set(cookie);
                }
            }
    
            protected void btn_Click(object sender, EventArgs e)
            {
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies["WWW-Username"];
                    cookie = new HttpCookie("WWW-Username");
                    cookie.Expires = DateTime.UtcNow.AddYears(-1);
                    //HttpContext.Current.Response.Cookies.Add(cookie);
                    HttpContext.Current.Response.Cookies.Set(cookie);
                    HttpContext.Current.Request.Cookies.Remove("WWW-Username");
                }
    
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                }
                Response.Redirect("home.aspx", false);
            }
    Site.Master.cs
    
    protected void Page_Load(object sender, EventArgs e) { }
    
    protected void Page_Init(object sender, EventArgs e)
            {
                if (HttpContext.Current.Request.Cookies["WWW-Username"] != null)
                {
                    string tmp2 = HttpContext.Current.Request.Cookies["WWW-Username"].Value.ToString();
                    //Response.Write(tmp2);
                }
            }

    And an empty page home.aspx with no code behind.

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 3:42 AM
  • User-195907812 posted

    Great, it works. Thank you!

    Friday, August 21, 2020 8:21 AM