ASP.NET Push Redirect on Session Timeout

前端 未结 10 1874
Happy的楠姐
Happy的楠姐 2020-11-29 21:04

I\'m looking for a tutorial, blog entry, or some help on the technique behind websites that automatically push users (ie without a postback) when the session expires. Any h

10条回答
  •  臣服心动
    2020-11-29 21:49

    Unfortunately it can't be done. The session timeout only occurs on the server side and you won't detect this until the user performs some kind of post back action.

    However, what you CAN do is to inject some HTML or JavaScript header code that will automatically push the user to a logout page in the same timeframe as your session timeout. This doesn't guarantee a perfect synch, and you may run into issues if your user is doing some time intensive items and you are not resetting the clock.

    I typically add this code to my Page_Load events to accomplish this.

    ' Register Javascript timeout event to redirect to the login page after inactivity
      Page.ClientScript.RegisterStartupScript(Me.GetType, "TimeoutScript", _
                                                  "setTimeout(""top.location.href = 'Login.aspx'""," & _
                                                   ConfigurationManager.AppSettings("SessionTimeoutMilliseconds") & ");", True)
    

提交回复
热议问题