Tuesday, March 1, 2011

Redirect to Error page when user clicks the Browser Refresh button

i need to check whether the user clicking the browser Refresh button and redirect to error page. Can we do this in javascript or any server side methods in ASP.net

From stackoverflow
  • If you give each link you present a unique ID (e.g. a GUID) in the URL as a parameter, then you can keep track of all the requests you've processed. (You could clear out "old" requests if you don't mind the mechanism not working if someone leaves a browser open for a few days and then hitting refresh.) The first time you see a GUID, write it into the table. If you see it again, redirect to an error page.

    It's pretty ugly though, and users could just edit the URL to change the GUID slightly. (You could fix this last flaw by recording the GUID when you generate it, and update the table to indicate when it's been used.)

    In general, users expect to be able to refresh the page though - particularly for GET requests (even though most users wouldn't know what that means). Why do you want to do this?

  • you can use client side hidden variable to store a counter or you can put counter in session. Well I would suggest you to expire the page on refresh there are ways you can achieve this disable cache etc [like all banks website do].

  • You can do that, but I'm sure you shouldn't. The user is in control of the browser, and if she feels like refreshing, it your job to make sure the page refreshes. Returning an error page is the wrong answer.

  • Well, you can use a very famous tecnique called "Syncronizing Token" or something like that =D, mostly used to send forms.

    This will work like this:

    1. Create a function to provide a pseudo-random string token.

    2. For every request to you page, check if a variable in Session, ex: Session["synctoken"] if present. If no, then it is the first time, generate a token and store it there.

    3. Every link request, ex: "mypage.aspx" put a get called synctoken with another token, diferent from the one you have stored in the Session, it goes like "mypage.aspx?synctoken=2iO02-3S23d".

    4. Then, comming back to (2), in a request, if a token is present in Session check if the GET is present (Request.QueryString["synctoken"] != null). If no, send Error. If yes check whether the Tokens (Session and GET) are different. If they are different, it is ok, store the GET into your Session (Session["synctoken"] = Request.QueryString["synctoken"]) and go to step (2). If no, then the user refreshed the page, there goes your error.

    It goes like:

    if (Session["synctoken"] != null) {
        if (Request.QueryString["synctoken"] != null) {
            if (Request.QueryString["synctoken"].ToString().Equals(Session["synctoken"].ToString())) {
                // Refresh! Goto Error!
                MyUtil.GotoError();
            }
            else {
                // It is ok, store the token and go on!
                Session["synctoken"] = Request.QueryString["synctoken"];
            }
        }
        else {
            MyUtil.GotoErrorPage();
        }
    }
    else {
        Session["synctoken"] = MyUtil.GenerateToken();
    }
    

    Sorry if I could not be more clear.. good luck!

    José Leal : Ok, got a minus but I don't even know why! Sorry to help.

0 comments:

Post a Comment