close session in mvc 4

1

I can not close an open session.

I use this method but it DOES NOT WORK.

<a>
href="@Url.Action("CerrarSesion","Home")"> <i class="icon-key"></i> Cerrar Sesión
</a>

Here I have the driver.

<HttpPost()>
    Function CerrarSesion() As ActionResult 
        If IsNothing(Session) = False Then 
            Session("usuario") = Nothing 
            Session("usu") = Nothing 
            Session.Clear() 
            Session.Abandon() 
        End If 
        FormsAuthentication.SignOut() 
        Return RedirectToAction("Index", "Home") 
    End Function 

When he uses this form he tells me that he "can not find the resource".

and here I have this function and use it on a button and it does not work ...

<script type = "text/javascript">
    function SessionAbandon() {
        $.ajax({ 
            type: "POST", 
            url: "/Home/CerrarSesion", 
            data: {}, 
            contentType: "application/json; charset=utf-8", 
            dataType: "json", 
            async: true, 
            error: function (XMLHttpRequest, textStatus, errorThrown) { 
                alert(textStatus + ": " + XMLHttpRequest.responseText); 
            } 
            }); 
    } 
</script>
    
asked by Heiner Said 07.12.2016 в 00:25
source

2 answers

0

For @Url.Action to work, what you have to do is remove <HttpPost()> from the controller.

@Url.Action is not designed to post and you do not need to post it either.

    
answered by 07.12.2016 / 14:10
source
1

The instruction FormsAuthentication.SignOut() is enough to loosen up, that can be validated with the variable context.User.Identity.IsAuthenticated . The only thing that I find strange there is that the ajax can not redirect you with a Return RedirectToAction("Index", "Home") you would have to return a HttpStatusCodeResult(401); // Unauthorized or simply assign it in the response Response.StatusCode = 401; and catch it in the success of the ajax or configure it in the ajax setup:

$.ajaxSetup({
    statusCode: {
    // unauthorized
    '401': function () {
      window.location.href = action.home.index;
    }
});
    
answered by 07.12.2016 в 06:41