views:

127

answers:

1

Here is the situation:

  1. User logs in via username/password stored in an MSSQL database
  2. If the user is authenticated, the system makes a session variable with username/SHA1'd password and boolean if the user is logged in or not (for subsequent pages)
  3. I need to be able to destroy the session variable. I want a confirmation box as well.

This is what I have so far:

<script type="text/javascript">
    //<![CDATA[
    function doLogout() {
        try {
            var conf = false;
            conf = confirm("Really log out?");
            if (conf === true) {
                $.post("logout.aspx");
            }
        } catch (ex) {
            alert(ex);
        }
    }
    //]]>
</script>

Since it is an ajax request won't reload the page (the functionality works fine, the request destroys the session), I think I need a different approach to do this. I would really like to be able to do it all in ASP.NET if possible.

Any solution is welcome, as long as #3 above is fulfilled.

+1  A: 

Well, for starters your solution depends on the user having Javascript, if they don't they won't be able to logout. I don't think you should be using AJAX for this, just a simple link/button to logout.aspx would be fine, that could then correctly redirect them to the homepage with the 'logged-out' state set. You could then use some unobtrusive Javascript to add the confirmation.

jQuery version (since you mentioned that):

<a href="logout.aspx" id="logout-link">Logout</a>
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function() {
        $('#logout-link').click(function () {
            return confirm("Really log out?");
        });
    });
    //]]>
</script>

pure-Javascript version:

<a href="logout.aspx" id="logout-link">Logout</a>
<script type="text/javascript">
//<![CDATA[
window.onload = function() {
     if(!document.getElementById) return;
     document.getElementById('logout-link').onclick(function() {
         return confirm("Really log out?");
    });
}
//]]>
</script>
roryf
Thanks! This works just perfect. I need to brush up on my javascript, its been a few years :P
Anders