Javascript 如何仅在 mvc3.net 中注销后禁用浏览器后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14437987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Disable Browser Back Button only after Logout in mvc3.net
提问by S.p
I am using FormsAuthentication for userlogin. I am having a problem after user logs out successfuly the back button is browser allows user to view pages. I tried using javascript
我正在使用 FormsAuthentication 进行用户登录。我在用户成功注销后遇到问题,后退按钮是浏览器允许用户查看页面。我尝试使用 javascript
<script type = "text/javascript" >
function preventBack() { window.history.forward(1); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
But back button is completly disabled. It worked bt,I dont want to disable back button functionality when user is logged in. i want my LOGGED IN user to use browser back button as normal. But once he choosed to log out, he is not allow to see any of contents by pressing Back. I also tried using
但后退按钮完全禁用。它起作用了,我不想在用户登录时禁用后退按钮功能。我希望我的登录用户像往常一样使用浏览器后退按钮。但是一旦他选择退出,他就不能按返回看到任何内容。我也尝试使用
Session.Abandon();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
But this is also not working.how do I fix this?
但这也不起作用。我该如何解决这个问题?
回答by Rich O'Kelly
You could clear the browser history when the user logs out:
您可以在用户注销时清除浏览器历史记录:
var url = window.location.href;
window.history.go(-window.history.length);
window.location.href = url;
However this would not be particularly robust - it relies on javascript, it would not work across multiple tabs and may only serve to annoy the user. IMO the best bet is to set appropriate caching headers such that the browser will not cache any of your 'logged in' pages via a NoCacheAttribute applied appropriately:
然而,这不会特别健壮——它依赖于 javascript,它不能跨多个选项卡工作,并且可能只会惹恼用户。IMO 最好的办法是设置适当的缓存标头,以便浏览器不会通过适当应用的 NoCacheAttribute 缓存您的任何“登录”页面:
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
回答by Arjun Ajith
Use this code in the html page on which you need to control the back button.
在需要控制后退按钮的 html 页面中使用此代码。
$().ready(function() {
if(document.referrer != 'http://localhost:8181/'){
history.pushState(null, null, 'login');
window.addEventListener('popstate', function () {
history.pushState(null, null, 'login');
});
}
});
This code will block back button event. The if condition is for allowing the back button if the previous page is 'http://localhost:8181/'. Back button won't be working if the previous page is not 'http://localhost:8181/'. If you need to block all previous pages then avoid the if condition. The history.pushStatestatements will replace the url on the browser address bar to 'login'. So I recommend you to change 'login' with your page url.
此代码将阻止后退按钮事件。if 条件是如果上一页是“ http://localhost:8181/” ,则允许后退按钮。如果上一页不是“ http://localhost:8181/”,则后退按钮将不起作用。如果您需要阻止所有以前的页面,请避免使用 if 条件。该history.pushState声明将取代浏览器的地址栏“登录”的网址。因此,我建议您使用页面 url 更改“登录”。
Advantages of this method:-
这种方法的优点:-
- No need to control the cache.
- We could allow the back button event for specified previous pages and could block the rest.
- 无需控制缓存。
- 我们可以允许指定前一页的后退按钮事件,并可以阻止其余页面。
Hoping my answer will help someone.
希望我的回答对某人有所帮助。
回答by surendran
Disabling back button is not a right way to achieve your need. Instead you can add the following three tags in your html file, which takes care of clearing cache.
禁用后退按钮不是满足您需求的正确方法。相反,您可以在 html 文件中添加以下三个标签,它负责清除缓存。
<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">
回答by Ashwini Verma
The easiest way I found is using OutputCache Attribute
我发现的最简单的方法是使用OutputCache 属性
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class HomeController : Controller
{
}
回答by shafi7468
<script language="JavaScript" type="text/javascript">
window.history.forward();
</script>
回答by Poornachander K
var url = window.history.forward();
window.history.go(-window.history.length);
回答by eKek0
If you want this for all your pages, you could write in your Global.asax:
如果您希望所有页面都这样,您可以在 Global.asax 中写入:
protected void Application_BeginRequest()
{
Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Expires", "0");
}
This will not cache any page of your site.
这不会缓存您网站的任何页面。
回答by alj
Please go through the article http://www.aspdotnet-suresh.com/2011/11/disable-browser-back-button.html. I used the javacript function provided by the author in my layout page to prevent back button issue , as i need to provide access to certain pages to all visitors of my website.
请阅读文章 http://www.aspdotnet-suresh.com/2011/11/disable-browser-back-button.html。我在我的布局页面中使用了作者提供的 javacript 函数来防止后退按钮问题,因为我需要向我网站的所有访问者提供对某些页面的访问权限。
This solution worked for me in IE 11 and Chrome Version 43.0.2357.130 m.
这个解决方案在 IE 11 和 Chrome 版本 43.0.2357.130 m 中对我有用。
Hope this helps.
希望这可以帮助。
回答by Ain Ronquillo
Please use this code in your Master Page Load Event.
请在您的母版页加载事件中使用此代码。
if(!IsPostBack)
{
if (Session["LoginId"] == null)
Response.Redirect("frmLogin.aspx");
else
{
Response.ClearHeaders();
Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
}
}
Hope it helps! :)
希望能帮助到你!:)

