How can I end a session when I click the back back button in asp.net

This is my scenario:

I have the following pages:

  • 1 Login Page
  • 1 main page as "ABC.Master"
  • 3 child pages like "page 1", "page 2", "page 3".

Page 1, page 2, and page 3 are child pages of the ABC.Master main page.

Page Stream:

  • After entering the username and password, if I click on Login, it will move to page 1
  • On page 1, if I click on some links, it moves to the page
  • On page 2, if I click the "Back in Browser" button, the session should be expired.

Note . This should be a pure banking site. ie) When you click on the "Back" button, the "Session" button has expired.

+4
source share
1 answer

This has been a problem for a while. Most people used this to get around this:

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

This piece of code basically directs the current page, which expires immediately after its publication, and sets that the page does not cache any of its contents.

However, some browsers may ignore the page cache settings, and some users still manage to get away with submitting the form several times.

Workaround:

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

    if (Page.IsPostBack){
        if (isPageExpired()){
           Response.Redirect("expired.htm");
        }
        else {
           Session("TimeStamp") = Now.ToString;
           ViewState("TimeStamp") = Now.ToString;
        }
    }
}


private boolean isPageExpired()
{
    if (Session("TimeStamp") == null || ViewState("TimeStamp") == null)
        return false;
    else if (Session("TimeStamp") == ViewState("TimeStamp"))
        return true;
    else
        return false;
}

, , , , , isPageExpired. true, ; , : , .

isPageExpired , , . , ; .

+4

Source: https://habr.com/ru/post/1524498/


All Articles