Cookies will not expire (noob question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • th3dude

    Cookies will not expire (noob question)

    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("myC ookie");
    }


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


    cookie.Expires = DateTime.Now.Ad dDays(1);
    Response.Cookie s.Add(cookie);


    lbl_welcome.Tex t = "<b>Cookie Created.</b>";
    lbl_welcome.Tex t = "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.Ad dMinutes(-1);
    }


    }


    What am i missing here, seems fairly simple?


    Thanks for any help!


  • Joern Schou-Rode

    #2
    Re: Cookies will not expire (noob question)

    On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnothom@gmail. comwrote:
    HttpCookie cookie = Request.Cookies["myCookie"];
    if (cookie != null)
    {
    cookie.Expires = DateTime.Now.Ad dMinutes(-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.Cookie s["myCookie"].Expires = DateTime.Now.Ad dDays(-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

    Comment

    • th3dude

      #3
      Re: Cookies will not expire (noob question)

      On Jul 24, 12:35 pm, "Joern Schou-Rode" <j...@malamute. dkwrote:
      On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail. comwrote:
                  HttpCookie cookie = Request.Cookies["myCookie"];
                  if (cookie != null)
                  {
                      cookie.Expires = DateTime.Now.Ad dMinutes(-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.Cookie s["myCookie"].Expires = DateTime.Now.Ad dDays(-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!

      Comment

      • th3dude

        #4
        Re: Cookies will not expire (noob question)

        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" <j...@malamute. dkwrote:
        On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail. comwrote:
                    HttpCookie cookie = Request.Cookies["myCookie"];
                    if (cookie != null)
                    {
                        cookie.Expires = DateTime.Now.Ad dMinutes(-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.Cookie s["myCookie"].Expires = DateTime.Now.Ad dDays(-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/

        Comment

        • th3dude

          #5
          Re: Cookies will not expire (noob question)

          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" <j...@malamute. dkwrote:
          On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail. comwrote:
                      HttpCookie cookie = Request.Cookies["myCookie"];
                      if (cookie != null)
                      {
                          cookie.Expires = DateTime.Now.Ad dMinutes(-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.Cookie s["myCookie"].Expires = DateTime.Now.Ad dDays(-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/

          Comment

          Working...