How to disable the browser back button in my mvc from only my login page?

♀尐吖头ヾ 提交于 2021-02-11 13:57:35

问题


I tried this method by putting it in my login.cshtml but it doesnt work there at all. Then I tried putting it in my _Layout.cshtml but then it does it's job while affecting the entire website (as expected). Any tips on where I should put or how I should manipulate the code?

here it is here:

function preventBack() { window.history.forward(); }
    setTimeout("preventBack()", 0);

回答1:


I recently used this in an MVC project. Maybe you can put it on the page that the login redirects to.

//kill all back button functionality
function noBack() { window.history.forward() }
noBack();
window.onload = noBack;
window.onpageshow = function(evt) { if (evt.persisted) noBack() }
window.onunload = function() { void (0) }

Do be careful though if you are using this for security reasons, as Javascript is not the most ideal solution to handle secure logic within a site. It is easy to get around since the Javascript code is executed on the clients PC and/or it can be disabled by the browser.




回答2:


You can put it in the layout but active it only if your on the login page :

 if(window.location.href.toLowerCase().indexOf("login") > -1)
 {
     function preventBack() { window.history.forward(); }
     setTimeout("preventBack()", 0);
 }


来源:https://stackoverflow.com/questions/27403062/how-to-disable-the-browser-back-button-in-my-mvc-from-only-my-login-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!