s.k.paul
s.k.paul

Reputation: 7291

Prevent user to go back after logout in asp.net mvc

I have tried all possible ways to prevent user to go back after logout. But none worked. I tried -

Response.ClearHeaders();

Response.Cache.SetAllowResponseInBrowserHistory(false);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCach‌​es);

Response.AppendHeader("Cache-Control", "no-cache"); 
Response.AppendHeader("Cache-Control", "private"); 
Response.AppendHeader("Cache-Control", "no-store"); 
Response.AppendHeader("Cache-Control", "must-revalidate"); 
Response.AppendHeader("Cache-Control", "max-stale=0");  
Response.AppendHeader("Cache-Control", "post-check=0"); 
Response.AppendHeader("Cache-Control", "pre-check=0");  
Response.AppendHeader("Pragma", "no-cache"); 
Response.AppendHeader("Expires", "Mon, 26 Jul 2000 05:00:00 GMT"); 

Response.Cache.SetCacheability(HttpCacheability.NoCache);  
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache"); 
Response.AppendHeader("Expires", "0"); 

I tested on chrome (v.56). The above code can not prevent user to go back after logout.

Any idea??

Upvotes: 2

Views: 4643

Answers (2)

Khalid
Khalid

Reputation: 645

Paste this line of Code Above the Controller on which you want to prevent go back

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

Like

 [System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        Session["Username"] = null;
        Session.RemoveAll();
        Session.Abandon();

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

Upvotes: 1

keerti
keerti

Reputation: 465

Go to the Global.asax.cs file and clear cache from the application by using given below code (hope this help) -

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}  

Upvotes: 5

Related Questions