[英]Browser back button navigates to login page when user is still logged in
I have a website that is mostly built in .NET MVC. 我有一个主要以.NET MVC内置的网站。 I say mostly because the login page is done using web forms. 我说这主要是因为登录页面是使用Web表单完成的。 The login page uses forms authentication. 登录页面使用表单身份验证。 The issue is that when I use the browser back button, it navigates back to the login page when the user is still authenticated. 问题是,当我使用浏览器的后退按钮时,当用户仍通过身份验证时,它会导航回到登录页面。 How can I stop this from happening? 我该如何阻止这种情况的发生?
I tried to set the cache to null on load of the login page, but no luck: 我试图在登录页面加载时将缓存设置为null,但是没有运气:
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Let me know if there is any code that may be helpful. 让我知道是否有任何有用的代码。 I've never dealt with this issue before and I'm not sure what would help. 我以前从未处理过此问题,也不确定会有什么帮助。
You could add a function that checks if they are logged in and if they are redirect them to the 'members' only page. 您可以添加一个功能,以检查它们是否已登录以及是否将其重定向到“仅成员”页面。 I cannot post any code because I am on a mobile device. 我在移动设备上,因此无法发布任何代码。
If you want to apply the "no cache on browser back" behavior on all pages then you should put it in global.asax. 如果要在所有页面上应用“浏览器后无缓存”行为,则应将其放在global.asax中。
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
The simplest way - disabling output cache (via attribute OutputCache) for action method which is responsible to show login page and also add condition which checks that user is already logged in: 最简单的方法-禁用操作方法的输出缓存(通过属性OutputCache),该方法负责显示登录页面,并添加条件以检查用户是否已登录:
public partial class AccountController : Controller
{
[HttpGet]
[OutputCache(NoStore = true, Duration = 0)]
public virtual ActionResult Index()
{
if (User.Identity.IsAuthenticated)
return RedirectToAction("Index", "Home");
LoginModel model = new LoginModel();
return View(model);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.