简体   繁体   中英

How to prevent back button after logout in ASP.NET Core MVC 3.1 app

I am working on ASP.NET Core MVC web application. I have created a controller for logout and it's working fine. I am trying to remove cache when user clicked on logout. I used the below given code but it is not working in case of cache removal. Here is my logout controller code:

[HttpGet]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public ActionResult Logout()
{
  HttpContext.Session.Clear();
  return RedirectToAction("Login", "Login");
}

I even tried to use code given below to remove cache but it is not working too.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

// Clears the session at the end of request
Session.Abandon();

How can I remove cache or prevent back button click when user logged out in Asp.Net Core app.

Try this

[HttpPost]            
[Route("logout")]         
public async Task<IActionResult> Logout()
{
  await HttpContext.SignOutAsync();
  return new JsonResult(new
  {
    //return whatever you want
  });
}

This can be achieved using Javascript. When you clicked on logout I assume you are showing an HTML page that says "You are successfully logged out" or some kind of it.

In that page add this script

<script type = "text/javascript" >  
    function preventBack() { window.history.forward(); }  
    setTimeout("preventBack()", 0);  
    window.onunload = function () { null };  
</script>

This will prevent the user from clicking on back button of the browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM