locked
Cookies are not deleting programatically using c# RRS feed

  • Question

  • User1064890599 posted

    Hello all,

     I have created a simple Intranet application, i create cookies programatically. But when a user delete it by clicking on the button, Cookies do not delete.

    Following is the code which i use to create cookies:-

    Response.Cookies["CkUSN"].Value = txtEmail.Text;

    Response.Cookies["CkPWD"].Value = txtPassword.Text;

    Response.Cookies["CkUSN"].Expires = DateTime.Now.AddDays(1);

    Response.Cookies["CkPWD"].Expires = DateTime.Now.AddDays(1); 

    Now when a user click on the button to delete cookies, Following code runs:-

    Response.Cookies["CkUSN"].Expires = DateTime.Now.AddYears(-1);

    Response.Cookies["CkPWD"].Expires = DateTime.Now.AddYears(-1);

    OnPage Load i have written  the following code:-

     if (Request.Cookies["CkPWD"] != null)                

    {                    

    String Usn, Pwd;

    Pwd = Request.Cookies["CkPWD"].Value;                    

    Usn = Request.Cookies["CkUSN"].Value;

    }

    When i click button to expire or delete cookies, and load the page again, OnPageLoad always get the cookies, means Cookies are not expiring or delete. So can anyone please help me to delete the cookies when pressing delete button in C#.net?.

    Timely help will be highly appreciated, Thanks in advance.

     

    Wednesday, April 17, 2013 6:26 AM

Answers

  • User-782344923 posted

    Hi again,

    I create an sample example accodring to your case. Working fine at every point. Follow the code as

    --------------
    ASPX MARKUP:
    --------------
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertDeleteCookies.aspx.cs"
        Inherits="Test_Web.InsertDeleteCookies" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Insert & Delete Cookies</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <p>
                <asp:TextBox ID="UserNameTextBox" runat="server" />
            </p>
            <p>
                <asp:Button ID="InsertButton" runat="server" Text="Insert To Cookie" OnClick="InsertButtonClick" />
                <asp:Button ID="DeleteButton" runat="server" Text="Delete From Cookie" OnClick="DeleteButtonClick" />
                <asp:Button ID="CheckButton" runat="server" Text="Check Cookie" OnClick="CheckButtonClick" />
            </p>
            <p>
                <asp:Label ID="ResultLabel" runat="server" />
            </p>
        </div>
        </form>
    </body>
    </html>
    
    -------------
    CODE BEHIND:
    -------------
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    
    namespace Test_Web
    {
        public partial class InsertDeleteCookies : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Request.Cookies["UserName"] == null)
                    {
                        Response.Cookies["UserName"].Value = "";
                        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
                    }
                }
            }
    
            protected void InsertButtonClick(object sender, EventArgs e) 
            {
                string userName = UserNameTextBox.Text.Trim();
    
                if (userName != "")
                {
                    Response.Cookies["UserName"].Value = userName;
                }
                else 
                {
                    ResultLabel.Text = "UserName must be enter!";
                    ResultLabel.ForeColor = Color.Red;
                    UserNameTextBox.Focus();
                }
            }
    
            protected void DeleteButtonClick(object sender, EventArgs e) 
            {
                if (Request.Cookies["UserName"] != null)
                {
                    Response.Cookies["UserName"].Value = "";
                    Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                }
                else 
                {
                    ResultLabel.Text = "No Cookie is present!";
                    ResultLabel.ForeColor = Color.Red;
                }
            }
    
            protected void CheckButtonClick(object sender, EventArgs e)
            {
                if (Request.Cookies["UserName"] != null)
                {
                    ResultLabel.Text = "Cookie is present!";
                    ResultLabel.ForeColor = Color.Red;
                }
                else
                {
                    ResultLabel.Text = "No Cookie is present!";
                    ResultLabel.ForeColor = Color.Green;
                }
            }
        }
    }

    It will help you to understand. Test this code in your enviournment.

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 7:47 AM

All replies

  • User1610357552 posted

       Response.Cookies.Remove("CkUSN");

    Wednesday, April 17, 2013 7:18 AM
  • User1064890599 posted

    Thanks for the reply,

    Saritapatil your code not working. I am afraid i already wasted alot of time. Please anyone immediately help, thanks.

    Wednesday, April 17, 2013 7:44 AM
  • User-782344923 posted

    Rauf abid

    Now when a user click on the button to delete cookies, Following code runs:-

    Response.Cookies["CkUSN"].Expires = DateTime.Now.AddYears(-1);

    Response.Cookies["CkPWD"].Expires = DateTime.Now.AddYears(-1);

    Hi Abid,

    Modify your Delete Cookie button code as

    Response.Cookies["CkUSN"].Expires = DateTime.Now.AddDays(-1);
    Response.Cookies["CkPWD"].Expires = DateTime.Now.AddDays(-1);

    Cheers

    Wednesday, April 17, 2013 8:02 AM
  • User1064890599 posted

    Thanks for the reply,

    Iam using AddYears(-1) and you are using AddDays(-1) to expire. In my way is it not deleting all cookies for last complete year ???. Is it necessary that if i create cookie that will expire after one day  like (.AddDays(1)),  then i must use your suggested code ???

    Also please tell me the difference between .AddDays(-1) and .AddYears(-1) and how both will function ???

    Really thanks for your help.

    Thursday, April 18, 2013 12:27 AM
  • User-782344923 posted

    Hi again,

    I added AddDays(-1) because during creating cookies of Username and Password, you set Cookie Expire Time as AddDays(1). So It is best practice that you should expire cookie with same attributes as AddDays(-1). 

    Cheers

    Thursday, April 18, 2013 4:27 AM
  • User1064890599 posted

    Thanks for the reply,

    I am still struggling because cookies are not deleting. I use the suggested code but no luck. What iam missing? iam using Latest version of Mozilla firefox and latest internet explorer.

    Following is the Cookie which i delete by writing the following line of code. I also deleted manually but still reading value on a particular page.

      Response.Cookies["CkPWD"].Expires = DateTime.Now.AddYears(-1);

    Now below line of code read the value when i try to get the Cookie which i deleted above


     if (Request.Cookies["CkPWD"] != null)

    {

    Here it comes and still read values

    }

    Please help.

    Thursday, April 18, 2013 6:05 AM
  • User1064890599 posted

    Please look the following code. Two same statements. When the first statement runs it must  delete the cookie [CKUSN] and should not go into the second statement under if else. But it again read the Cookie in the second statement under if else. What iam missing plz guide immediately.

     

      if (Request.Cookies["CkUSN"] != null)            

     {  Response.Cookies["CkUSN"].Expires = DateTime.Now.AddDays(-1);  }

     

     if (Request.Cookies["CkUSN"] != null)            

    { Here it comes again which is the proof that cookie not deleted. }

    Thursday, April 18, 2013 6:48 AM
  • User-2125168801 posted

    How about below code ?

    if (Request.Cookies["UserSettings"] != null)
    {
        HttpCookie myCookie = new HttpCookie("UserSettings");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(myCookie);
    }
    
    Link : http://msdn.microsoft.com/en-us/library/ms178195.aspx
    Thursday, April 18, 2013 7:29 AM
  • User-782344923 posted

    Hi again,

    I create an sample example accodring to your case. Working fine at every point. Follow the code as

    --------------
    ASPX MARKUP:
    --------------
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertDeleteCookies.aspx.cs"
        Inherits="Test_Web.InsertDeleteCookies" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Insert & Delete Cookies</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <p>
                <asp:TextBox ID="UserNameTextBox" runat="server" />
            </p>
            <p>
                <asp:Button ID="InsertButton" runat="server" Text="Insert To Cookie" OnClick="InsertButtonClick" />
                <asp:Button ID="DeleteButton" runat="server" Text="Delete From Cookie" OnClick="DeleteButtonClick" />
                <asp:Button ID="CheckButton" runat="server" Text="Check Cookie" OnClick="CheckButtonClick" />
            </p>
            <p>
                <asp:Label ID="ResultLabel" runat="server" />
            </p>
        </div>
        </form>
    </body>
    </html>
    
    -------------
    CODE BEHIND:
    -------------
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    
    namespace Test_Web
    {
        public partial class InsertDeleteCookies : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Request.Cookies["UserName"] == null)
                    {
                        Response.Cookies["UserName"].Value = "";
                        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
                    }
                }
            }
    
            protected void InsertButtonClick(object sender, EventArgs e) 
            {
                string userName = UserNameTextBox.Text.Trim();
    
                if (userName != "")
                {
                    Response.Cookies["UserName"].Value = userName;
                }
                else 
                {
                    ResultLabel.Text = "UserName must be enter!";
                    ResultLabel.ForeColor = Color.Red;
                    UserNameTextBox.Focus();
                }
            }
    
            protected void DeleteButtonClick(object sender, EventArgs e) 
            {
                if (Request.Cookies["UserName"] != null)
                {
                    Response.Cookies["UserName"].Value = "";
                    Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                }
                else 
                {
                    ResultLabel.Text = "No Cookie is present!";
                    ResultLabel.ForeColor = Color.Red;
                }
            }
    
            protected void CheckButtonClick(object sender, EventArgs e)
            {
                if (Request.Cookies["UserName"] != null)
                {
                    ResultLabel.Text = "Cookie is present!";
                    ResultLabel.ForeColor = Color.Red;
                }
                else
                {
                    ResultLabel.Text = "No Cookie is present!";
                    ResultLabel.ForeColor = Color.Green;
                }
            }
        }
    }

    It will help you to understand. Test this code in your enviournment.

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 7:47 AM
  • User1083584480 posted

    cookie will get deleted once your response send to the client machine.

    in the code if you are placing both statements before sending response back for first statement. it will definatly go into second statement and read the cookie.

    i hope this will help you to understand

    Thursday, April 18, 2013 8:15 AM
  • User1064890599 posted

    This code Already used. Can you please clarify me that is there any problem in code or there is something difference in browsers or browsers versions that can behave like this ???, thanks

    Thursday, April 18, 2013 8:19 AM
  • User1064890599 posted

    This code Already used. Can you please clarify me that is there any problem in code or there is something difference in browsers or browsers versions that can behave like this ???, thanks

    Thursday, April 18, 2013 8:41 AM
  • User1610357552 posted

    page1------------------------------ 

      HttpCookie ProductInformation = new HttpCookie("ProductInfo");
              ProductInformation["ProductOldPrice"] = decProdOldPrice.ToString();
                ProductInformation["ProductNewPrice"] = decProdNewPrice.ToString();
         Response.Cookies.Add(ProductInformation);

    page2---------------------------------

     HttpCookie ProductInformation = Request.Cookies["ProductInfo"];
                Label13.Text = ProductInformation["ProductOldPrice"].ToString();
                Label14.Text = ProductInformation["ProductNewPrice"].ToString();


    *--------------------------------------------------------------------------------------*  

      Request.Cookies.Remove("ProductInfo");

    Saturday, April 20, 2013 6:20 AM