【问题标题】:How to prevent back button after logout in ASP.NET Core MVC 3.1 app如何在 ASP.NET Core MVC 3.1 应用程序中注销后防止返回按钮
【发布时间】:2020-04-10 10:28:37
【问题描述】:

我正在开发 ASP.NET Core MVC Web 应用程序。我为注销创建了一个控制器,它工作正常。当用户单击注销时,我正在尝试删除缓存。我使用了下面给定的代码,但在缓存删除的情况下它不起作用。这是我的注销控制器代码:

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

我什至尝试使用下面给出的代码来删除缓存,但它也不起作用。

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();

当用户在 Asp .Net Core 应用程序中注销时,如何删除缓存或防止单击返回按钮。

【问题讨论】:

  • Session.Clear() 只是清除会话变量,它不会将任何人注销。用户保持登录状态
  • 查看Introduction to Identity 文档。注销页面已经可用。如果需要,您可以自定义它。这在Logout section 中显示。实际的注销操作是通过调用 SigningManager.SignoutAsync 来执行的
  • @PanagiotisKanavos 如何在我的代码中使用 SignOutAsync()?

标签: c# asp.net api asp.net-core-mvc visual-studio-2019


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    这可以使用 Javascript 来实现。当您单击注销时,我假设您正在显示一个 HTML 页面,上面写着“您已成功注销”或类似的内容。

    在该页面中添加此脚本

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

    这将阻止用户点击浏览器的后退按钮

    【讨论】:

    • 但是当我右键单击chrome中的后退按钮时,我可以返回
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2020-11-21
    • 2023-02-21
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多