问题
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