简体   繁体   中英

Forward button after login asp.net

I am developing a website where i have a Login page.Once the user is validated,he will be redirected to User's home Page. If the user clicks on browser's back button,he will be redirected to login page because of the browser cache.Now from Login page if he click on brower forward button,because of browser cache he is able to see his home page without his credentials validated in login page.How do i avoid this?

Avoid moving back to Login page using this script in your login page..

<script type = "text/javascript" >
        function preventBack() { window.history.forward(); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
</script>

We can also achieve this by disabling browser caching in code behind write the following lines of code in Page_Init event or Page_Load event and don't forgot to add namespace using System.Web; because HttpCacheability related to that namespace.

 protected void Page_Init(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}

Here this JavaScript functionality will work in all browsers and prevent users navigating back to previous page by hitting on browser back button check below piece of JavaScript code

<script type="text/javascript" language="javascript">
 function DisableBackButton() {
   window.history.forward()
  }
 DisableBackButton();
 window.onload = DisableBackButton;
 window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
 window.onunload = function() { void (0) }

Have you tried to debug this scenario ?

If user navigates back to login , you can deal with that scenario in page/control page_load event.

Alternatively if you are using visual studio, it is coming with boilerplate asp.net web application templates along with membership-ready implementation.

@jameem, @nayeem We can't disable browsers back/forward button for such scenario, web is all about letting users click/move anywhere on the page, it's all about how you design solution with care.

Javascript is a language of the DOM, we better utilize it to deal with this part only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM