简体   繁体   English

当用户仍然登录时,浏览器后退按钮导航到登录页面

[英]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.

相关问题 单击浏览器后退按钮后,注销仍可访问授权页面 - Logout still access to authorize page when click on browser back button 如何停止已经登录的用户使用浏览器的后退按钮再次登录? - How do I stop already logged in user to login again using back button of the browser? 当我单击浏览器的后退按钮时,我仍然返回上一页 - When i click on back button of the browser i still get back to back page 注销后,如何在ASP.NET MVC3中单击浏览器的后退按钮时将用户重定向到登录页面 - After logout,how to redirect user to login page when he/she will click back button of browser in ASP.NET MVC3 当用户导航离开页面或退出浏览器时,如何在ASP.net中销毁会话 - How to destroy session in ASP.net when user navigates away from the page or exits the browser 如果用户已登录,则画布页面显示登录按钮evem - canvas page showing login button evem if the user is logged in 用户登录后如何隐藏登录按钮? - How do I hide a login button when user is logged in? 用户未登录时(asp)重定向到登录页面 - Redirect to login-page when user is not logged in (asp) 当用户单击asp.net中的浏览器后退按钮时,如何快速重定向页面 - How to redirect the page quickly when the user clicks the browser back button in asp.net 用户单击按钮时重定向到登录页面 - Redirecting to login page when user clicks a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM