Jonathan
Jonathan

Reputation: 1669

how to log out the current user in asp.net mvc

I have used the below link to end a session.

http://pure-essence.net/2010/02/14/jquery-session-timeout-countdown/

In that i am able to redirect to the login page. But the user is not logging off. Just it is redirecting to that page. But it is not logging out. How can i log out the session in that case. Any help is appreciated.

Upvotes: 2

Views: 13739

Answers (2)

Nishan
Nishan

Reputation: 4421

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

This should work in newer versions.

Upvotes: 2

Yasser Shaikh
Yasser Shaikh

Reputation: 47774

Short Answer : Use FormsAuthentication.SignOut();

Long Answer

public ActionResult LogOff()
{
    FormsAuthentication.SignOut();

    return RedirectToAction("Login", "Account");
}

private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

Use [Authorize] above the actions which you don't want the user to do after logging out. This you can do by adding this attribute above actions individually or marking the entire class with [Authorize] so that all methods can now be only be accessed by authenticated users.

Hope this helps.

Upvotes: 3

Related Questions