Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to delete/modify cookie

Status
Not open for further replies.

wau25

Programmer
Mar 12, 2004
21
US
I have a cookie that has been set a value and expiration date to be a year from the date created. I am not able to change or delete the cookie. below is my code:

private void RDCookie()
{
Request.Cookies.Remove("SaveEmail2");
Response.Write(Request.Cookies["SaveEmail2"].Value);
}
The second line will fail because the cookie was removed before it could be displayed. However, if I have this:
private void Page_Load()
{
Request.Cookies.Remove("SaveEmail2");
}

and a button click event:
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write(Request.Cookies["SaveEmail2"].Value);
}
it'd still display the value. Same thing with modifying the cookie value. If I read the cookie value immediately after modification it shows the new value but after I refreshes the page the value is changed to the old value.
 
I think this is because when you remove the cookie from the request collection it is not removed from the response collection as well.

I'm not 100% on this as I havent tested it but it seems the most likely reason for what your experiencing.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Try adding a negative date to the cookie expiration date.
C.Expires = DateTime.Now.AddDays(-1)
Response.Cookies.Add(C)
That I believe is the only way to remove a cookie.
Marty
 
This should help,

Deleting Cookies
Deleting a cookie — physically removing it from the user's hard disk — is a variation on modifying it. You cannot directly remove a cookie because the cookie is on the user's computer. However, you can get the browser to delete the cookie for you. The technique is to modify the cookie as described above (that is, create a new cookie with the same name) but to set the cookie's expiration to a date earlier than today. When the browser checks the cookie's expiration, the browser will discard the now-outdated cookie.
from
Marty
 
This is what I tried and it still doesn't work :(

HttpCookie cookie = new HttpCookie("SaveEmail");
cookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie);
Request.Cookies.Add(cookie);
Request.Cookies["SaveEmail"].Expires = DateTime.Now.AddYears(-1);
Response.Cookies["SaveEmail"].Expires = DateTime.Now.AddYears(-1);
Response.Write("test:" + Request.Cookies["SaveEmail"].Value);
 
wau25,
Run this,
Code:
<%@ Page Language="C#" %>
<script runat="server">
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblCookieValue.Text = "Play with cookies";
        }
    }

    void btnSetCookie_Click(object sender, EventArgs e) {
        SetCookie(txtCookieName.Text, txtCookieValue.Text ,Convert.ToInt32(txtCookieExpire.Text));
    }

    public bool SetCookie(string cookiename, string cookievalue ,int iDaysToExpire)
    {
    try
    {
        HttpCookie objCookie = new HttpCookie(cookiename);
        Response.Cookies.Clear();
        Response.Cookies.Add(objCookie);
        objCookie.Values.Add(cookiename,cookievalue);
        DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
        Response.Cookies[cookiename].Expires =dtExpiry;
    }
        catch( Exception e){
        return false;
    }
        return true;
    }

    void btnGetCookie_Click(object sender, EventArgs e) {
        lblCookieValue.Text = GetCookie(txtCookieName.Text);
    }

    public string GetCookie(string cookiename){
    string cookyvalue="";
    try{
        cookyvalue= Request.Cookies[cookiename].Value;
    }
        catch(Exception e){
        cookyvalue="";
    }
        return cookyvalue;
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Label id="Label1" runat="server">Cookie Name </asp:Label>
        <asp:TextBox id="txtCookieName" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label id="Label2" runat="server">Cookie Value </asp:Label>
        <asp:TextBox id="txtCookieValue" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label id="Label3" runat="server">Cookie Expires</asp:Label>
        <asp:TextBox id="txtCookieExpire" runat="server"></asp:TextBox>
        <asp:Label id="Label4" runat="server">Less then or equal to 0 will remove the cookie </asp:Label>
        <br />
        <br />
        <asp:Button id="btnSetCookie" onclick="btnSetCookie_Click" runat="server" Text="Set Cookie"></asp:Button>
        <br />
        <br />
        <asp:Button id="btnGetCookie" onclick="btnGetCookie_Click" runat="server" Text="Get Cookie"></asp:Button>
        <br />
        <br />
        <br />
        <asp:Label id="lblCookieValue" runat="server">Label</asp:Label>
    </form>
</body>
</html>
Marty
 
The code works with new cookie members but not with this specific cookie I had named "SaveEmail". The current value of the cookie is 1 and I want to set it to "True". The expiration date of the cookie was set to be 1 year from the date it was created. The test code was the only code on my test page and I have checked the Global.asax to make sure that there isn't a line of code some where that resets the value of the cookie to 1 when the page loads.
 
Waua25, Just create a new cookie with the same name ("CookieEmail") with the new value and expire you want. It will overwrite the existing cookie and you should be all set. If you want to remove it do the same just give it an expire in the past.
Hope this helps,
Marty
 
I have tried all that and it still doesn't work. It works for all other cookies except this particular one, very strange. I give up and simply create a new cookie called "RememerEmail" and use that value instead. Thank you all for trying to help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top