0

I am in Session_End() of Global.asax.cs

There is an operation I need to do only if the session was ended on a specific page. The parameters (sender and eventArgs) are not of any help. Is there a way to figure out last page the user was on?

Thanks in advance.

Pallavi
  • 544
  • 6
  • 15

2 Answers2

0

You can probably use HttpContext.Request property but not sure whether that will still be valid after session expire.

In such case, you need to explicitly write the page URL in cookies every time user request a new page. That way, even though session expires you can check the cookie object and see what was the last requested page.

General scenario, once session expires end user should be re-authenticating himself and thus you should redirect to login page directly and if you are using Forms Authentication then you can just say FormsAuthentication.RedirectToLoginPage();

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • The problem is, I don't want to do a login redirect. It's a database operation that only needs to trigger if session timed out on a specific page. Let me see if HttpContext.Request property helps. – Pallavi Sep 13 '16 at 21:43
  • 1
    So you just need the last requested page? In that case as suggested, you can as well store the pagename or pageurl in cookies. – Rahul Sep 13 '16 at 21:44
0

You can store the URL of the last page accessed in Session with each request. That way when the session ends (global.asax Session_End) you can read what page the user had last visited before the session ended.

That's because most events are triggered by a request, but unless it's explicitly abandoned the session end is caused by a lack of activity. It's determined by the server when there hasn't been a request for a period. By definition there won't be any communication coming from the user indicating that the session is ending. They may have closed their browser five minutes ago.

(That's only if the session-state HttpSessionState.Mode property value is InProc. Otherwise the event won't fire.)

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62