Cookies will not expire (noob question)

Discussion in 'ASP .Net' started by th3dude, Jul 24, 2008.

  1. th3dude

    th3dude Guest

    This seems like an easy one, but i'm stuck on getting rid of a cookie
    and cannot seem to make it happen.

    I'm creating a cookie like so:


    private void createCookie()
    {
    HttpCookie cookie = Request.Cookies["myCookie"];
    if (cookie == null)
    {
    cookie = new HttpCookie("myCookie");
    }


    cookie["Name"] = "myName";
    cookie["PW"] = "myPw";


    cookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(cookie);


    lbl_welcome.Text = "<b>Cookie Created.</b>";
    lbl_welcome.Text = "Logged in As: " + cookie["Name"] + "
    Pw: " + cookie["PW"];


    }


    Then upon clicking a button i'm trying to get rid of the cookie (but
    it does not go away):


    private void doLogout()
    {


    HttpCookie cookie = Request.Cookies["myCookie"];
    if (cookie != null)
    {
    cookie.Expires = DateTime.Now.AddMinutes(-1);
    }


    }


    What am i missing here, seems fairly simple?


    Thanks for any help!
     
    th3dude, Jul 24, 2008
    #1
    1. Advertising

  2. On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <> wrote:

    > HttpCookie cookie = Request.Cookies["myCookie"];
    > if (cookie != null)
    > {
    > cookie.Expires = DateTime.Now.AddMinutes(-1);
    > }


    In the code quoted above, you read a cookie from the request (the input
    stream) and you modify it, but you do not write the changes to the
    response (the output stream). I believe you should be able to do something
    like the following _instead_ of what you have above:

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

    Somewhat off-topic: from the code you are showing, it seems that you are
    implementing some kind of authentication/login scheme. If this is the
    case, you might be happy to know that ASP.NET has several authentication
    implementations built right in - the one called "forms authentication"
    might just save you a lot of work :)

    --
    Joern Schou-Rode
    http://malamute.dk/
     
    Joern Schou-Rode, Jul 24, 2008
    #2
    1. Advertising

  3. th3dude

    th3dude Guest

    Hi Joern,

    You're tip fixed my cookie issue :)

    Many thanks also for the advice on using built-in authentication, i'll
    take of the help i can get.

    Cheers!



    On Jul 24, 12:35 pm, "Joern Schou-Rode" <> wrote:
    > On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <> wrote:
    > >             HttpCookie cookie = Request.Cookies["myCookie"];
    > >             if (cookie != null)
    > >             {
    > >                 cookie.Expires = DateTime.Now.AddMinutes(-1);
    > >             }

    >
    > In the code quoted above, you read a cookie from the request (the input  
    > stream) and you modify it, but you do not write the changes to the  
    > response (the output stream). I believe you should be able to do something  
    > like the following _instead_ of what you have above:
    >
    > Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);
    >
    > Somewhat off-topic: from the code you are showing, it seems that you are  
    > implementing some kind of authentication/login scheme. If this is the  
    > case, you might be happy to know that ASP.NET has several authentication  
    > implementations built right in - the one called "forms authentication"  
    > might just save you a lot of work :)
    >
    > --
    > Joern Schou-Rodehttp://malamute.dk/
     
    th3dude, Jul 24, 2008
    #3
  4. th3dude

    th3dude Guest

    Hi Joern,

    Your tip fixed my cookie issue :)


    Many thanks also for the advice on using built-in authentication,
    i'll
    take of the help i can get.


    Cheers!


    On Jul 24, 12:35 pm, "Joern Schou-Rode" <> wrote:
    > On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <> wrote:
    > >             HttpCookie cookie = Request.Cookies["myCookie"];
    > >             if (cookie != null)
    > >             {
    > >                 cookie.Expires = DateTime.Now.AddMinutes(-1);
    > >             }

    >
    > In the code quoted above, you read a cookie from the request (the input  
    > stream) and you modify it, but you do not write the changes to the  
    > response (the output stream). I believe you should be able to do something  
    > like the following _instead_ of what you have above:
    >
    > Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);
    >
    > Somewhat off-topic: from the code you are showing, it seems that you are  
    > implementing some kind of authentication/login scheme. If this is the  
    > case, you might be happy to know that ASP.NET has several authentication  
    > implementations built right in - the one called "forms authentication"  
    > might just save you a lot of work :)
    >
    > --
    > Joern Schou-Rodehttp://malamute.dk/
     
    th3dude, Jul 24, 2008
    #4
  5. th3dude

    th3dude Guest

    On Jul 24, 12:35 pm, "Joern Schou-Rode" <> wrote:
    > On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <> wrote:
    > >             HttpCookie cookie = Request.Cookies["myCookie"];
    > >             if (cookie != null)
    > >             {
    > >                 cookie.Expires = DateTime.Now.AddMinutes(-1);
    > >             }

    >
    > In the code quoted above, you read a cookie from the request (the input  
    > stream) and you modify it, but you do not write the changes to the  
    > response (the output stream). I believe you should be able to do something  
    > like the following _instead_ of what you have above:
    >
    > Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);
    >
    > Somewhat off-topic: from the code you are showing, it seems that you are  
    > implementing some kind of authentication/login scheme. If this is the  
    > case, you might be happy to know that ASP.NET has several authentication  
    > implementations built right in - the one called "forms authentication"  
    > might just save you a lot of work :)
    >
    > --
    > Joern Schou-Rodehttp://malamute.dk/


    Hi Joern,

    Your tip fixed my cookie issue :)


    Many thanks also for the advice on using built-in authentication,
    i'll take all of the help i can get.


    Cheers!
     
    th3dude, Jul 24, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey Powell

    Forms Authentication Cookies Never Expire

    Joey Powell, Dec 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    6,969
    Mike Moore [MSFT]
    Dec 4, 2003
  2. Mel
    Replies:
    3
    Views:
    486
    Toby Inkster
    Mar 16, 2005
  3. _Who
    Replies:
    7
    Views:
    2,752
  4. S. Bourbeau

    IE seems to expire all cookies instead of one

    S. Bourbeau, Dec 5, 2003, in forum: ASP General
    Replies:
    0
    Views:
    134
    S. Bourbeau
    Dec 5, 2003
  5. Tongass Park Neighborhood Association, Juneau Alas

    Cookies expire immediately, not when set to expire

    Tongass Park Neighborhood Association, Juneau Alas, Oct 1, 2009, in forum: ASP General
    Replies:
    2
    Views:
    1,238
    SQLDude
    Nov 24, 2009
Loading...

Share This Page