views:

1052

answers:

3

I have a login link that fires a javascript function that calls a logout page. This is what the logout page consists of:

 If Response.Cookies.Count > 0 Then
    Response.Cookies("aLog").Value = Nothing
    Response.Cookies.Clear()
End If

Originally I just had cookies.clear in there, but that wasn't working.

Here is the javascript that send the request to the logout page:

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function() {
        $('#logout-link').click(function() {
            if (confirm("Really log out?")) {
                $.cookie('aLog', null);
                location.href = $(this).attr('href');
            }
            return false;
        });
    });
    //]]>
</script>

The jQuery function $.cookie does not work either. The cookie is set by ASP.NET, so I figured I could unset it with ASP.NET too but apparently not. Any ideas/suggestions?

+2  A: 

Working with cookies in ASP.NET can be a little un-intuitive. To kill a cookie that already lives on the client-side, you have to set its expiration date to sometime in the past, and re-send the client the new cookie. The client browser will update the existing cookie with the new expiration date, and then immediately kill it since it has already passed the expiration date:

HttpCookie cookie = Request.Cookies["aLog"];
cookie.Expires = DateTime.Now.AddYears(-10);
Response.AppendCookie(cookie);
Rex M
Ok, so I get to my logout page, it says no cookies are set (request.cookies.count = 0), but when it redirects to the login page the cookie reappears? how annoying.
Anders
A: 

In addition to what Rex said, you should always be setting the ‘path’ of any cookie you use (usually to something like "/"). Otherwise, the visibility of the cookie depends on folder name in the URL, so if you have anything but a flat URL scheme setting and deleting cookies will work very unpredictably.

bobince
A: 

I had the same problem with Firefox 3, the cookie was dropped in the top of the page in clear text when the user where downloading on my ASP.Net web page.

For my part, using only the

Response.Cookies.Clear()

after my download call worked perfectly.

ForceMagic