locked
How to end cookies using a button RRS feed

  • Question

  • User-1557173817 posted

    Hello just went through mikes tutorial on adding a shopping cart to the bakery template which is associated with cookies. 

     cartId = Request.Cookies["cart"].Value.AsInt();

    How can i clear/expire cookies for a particular cart using a button rather than letting it expire at a particular data/time.

    Response.Cookies["cart"].Expires = DateTime.Now.AddMonths(6);
    Friday, November 27, 2015 11:20 AM

All replies

  • User-821857111 posted

    If you want to get rid of a cookie, set its expiry time to a point in the past.

    Response.Cookies["cart"].Expires = DateTime.Now.AddMinutes(-1);
    Friday, November 27, 2015 1:12 PM
  • User-1557173817 posted

    Ok thanks, but how can i do that using a button. I mean using the above code to trigger on click of a button to end cookies  for a particular user.

    Friday, December 4, 2015 4:48 PM
  • User-821857111 posted

    I'm not clear what you want to achieve. If you want a kind of control panel where you can manage the cookies for individual users, that's not possible. If you want to give the user the opportunity to clear their cookie on a button click, you can do that purely with javascript. Here's a button that points to a javascript function when it is clicked:

    <button onclick="expireCookie()">Clear cookie</button>

    And here's the function:

    <script>
        function expireCookie(){
            document.cookie = "cart=; expires=Thu, 01 Jan 1970 00:00:00 GMT"
        }
    </script>

    Friday, December 4, 2015 10:02 PM
  • User-1980594115 posted

    Here is an example you can use:

    <button type="button" onclick='document.cookie="cart=;expires=Wed 01 Jan 1970"'>Delete Cookie</button>

    Friday, December 4, 2015 10:29 PM
  • User-1327394822 posted
    if(IsPost){
            cookieByUser = Request["cookieByUser"];
            Response.Cookies["username"].Value = cookieByUser;
            Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
    
            if(!Request["deleteCookie"].IsEmpty()){
                 if(Request.Cookies["username"] != null){
                     Response.Cookies["username"].Expires = DateTime.Now.AddYears(-1);
    
                }
            }
            //Just a redirect so that you can come back to this page and check the cookie
            Response.Redirect("~/");
            
        }

    Create a form... In the above case I used a checkbox. if checked, first validate the checkbox, then if cookie exists and then expire the cookie by supplying back date.

    Saturday, December 5, 2015 9:36 AM
  • User-1327394822 posted
    <form method="post" style="max-width: 250px; border: 1px solid #efefef; padding: 5px;">
                    <div class="form-group">
                        <label for="cookieByUser">Email address</label>
                        <input type="text" class="form-control" name="cookieByUser" value="@cookieUserName" placeholder="Email">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                    <div class="checkbox">
                    <label>
                      <input type="checkbox" value="true" name="deleteCookie"> Delete Cookie
                    </label>
                  </div>
                </form>

    Saturday, December 5, 2015 9:38 AM
  • User-1557173817 posted

    Thanks guys. Am ending the cookie as a method of checking out for the bakery template.

    Monday, December 14, 2015 11:32 AM
  • User-1557173817 posted

    hello I tried the above code but the cookie still exist.

    here is my code:

      @{
                           if(IsPost){
            var cookieByUser = WebSecurity.CurrentUserName;
            Response.Cookies["cart"].Value = cookieByUser;
            Response.Cookies["cart"].Expires = DateTime.Now.AddMinutes(-1);
     
            if(!Request["deleteCookie"].IsEmpty()){
                 if(Request.Cookies["cart"] != null){
                     Response.Cookies["cart"].Expires = DateTime.Now.AddYears(-1);
     
                }
            }
            //Just a redirect so that you can come back to this page and check the cookie
           
            
        }
     
                           }
     
                            <form method="post" style="max-width: 250px; border: 1px solid #efefef; padding: 5px;">
                    
                    <button type="submit" class="btn btn-default">Submit</button>
                    <div class="checkbox">
                    <label>
                      <input type="checkbox" value="true" name="deleteCookie"> Delete Cookie
                    </label>
                  </div>
                </form>

    Wednesday, January 27, 2016 3:19 PM