Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear session on Logout MVC 4

I need to know how to clear session on Logout in MVC 4 asp.net I have tried almost every thing but all in vain.

    [AllowAnonymous]
    public ActionResult SignOut()
    {
        Response.AddHeader("Cache-Control", "no-cache, no-store,must-revalidate");
        Response.AddHeader("Pragma", "no-cache");
        Response.AddHeader("Expires", "0");
        Session.Abandon();

        Session.Clear();
        Response.Cookies.Clear();
        Session.RemoveAll();

        Session["Login"] = null;
        return RedirectToAction("Index", "Login");
    }

Session.Clear();
Session.Abandon();
Clear cookies
set session to Null on Logout etc

but when i click on Browser's back button . it redirect to the same account. please help me , how would i clear session variable when i click on logout button. I tried each of the above one by one and then in group but same result.

like image 961
Abdul Avatar asked Sep 30 '22 02:09

Abdul


1 Answers

When a user clicks the back button, the browser shows the cached version of the page, which is exactly what the user saw when he first downloaded that page. However, there is no contact with the sever at all to show this cached version of the page. The cached version of the page will look exactly as when it was downloaded, so you can see the user that was logged on when the page was downloaded. However, that doesn't mean that user is still logged in.

If you want to avoid the user to navigate back to previous cached pages, the easier way to do it is to remove the browser's history when the user logs out. You cannot do directly that, but there is a workaround that gives the same result. Run this JavaScript:

var Backlen=history.length;   history.go(-Backlen); 
window.location.href='new page url';

So, what you need to do is to return a page with this JavaScript, and, where you read 'new page url' you have to insert the page where you want to redirect the user when he logs out.

Of course, avoiding caching, like in Ravinder Singh's answer is an option, but it's not a good idea to avoid the browser from caching all the pages.

like image 104
JotaBe Avatar answered Dec 01 '22 08:12

JotaBe