locked
Force redirect on the client RRS feed

  • Question

  • User1310055179 posted

     How can I force a redirect on the client that is the same length as the session time-out by using a client script?

    Tuesday, September 4, 2018 10:39 AM

All replies

  • User475983607 posted

    Use one of the JavaScript timer functions.  Also, this is well covered subject that a basic Google search will reveal. 

    https://www.w3schools.com/howto/howto_js_countdown.asp

    The idea is simple.  Use server side code to populate the DOM with the current Session timeout value on each request.  The JavaScript app uses this value to setup the timer.

    As always, post your code.  Explain what you expect to happen and what actually happens.  Include any error messages.

    Tuesday, September 4, 2018 10:52 AM
  • Tuesday, September 4, 2018 11:57 AM
  • User283571144 posted

    Hi qsoft_developer,

    How can I force a redirect on the client that is the same length as the session time-out by using a client script?

    According to your description, I suggest you could firstly get the session timeout value when page load, then you could use javascript setInterval function to redirect user to login page.

    More details, you could refer to below ASP.NET example:

    Auto redirect to login page when Session is expired

    Best Regards,

    Brando

    Wednesday, September 5, 2018 6:31 AM
  • User1310055179 posted

    Hi mudassarkhan,

    I tried using your solution. Can you please explain why there is a check for IsPostback on the Page_Load?

    if (!this.IsPostBack)
                {
                    Session["Reset"] = true;
                    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
                    SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
                    int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert""SessionExpireAlert(" + timeout + ");"true);
                }

    The session is not reseted automatically on each postback.

    What happenes now is that the user is asked to reset the session after it's timeout, even if he keeps working with the system and posting back to the server.

    Doesn't it suppose to reset it whenecer the user keeps posting back to the server?

    My main purpose is to prevent situations when users doesn't log out of the system and someone can view sensitive data that was left open.

    Wednesday, September 12, 2018 8:17 AM
  • User283571144 posted

    Hi qsoft_developer,

    The session is not reseted automatically on each postback.

    What happenes now is that the user is asked to reset the session after it's timeout, even if he keeps working with the system and posting back to the server.

    Doesn't it suppose to reset it whenecer the user keeps posting back to the server?

    As far as I know, the session expired time will refresh when the user access page again.

    Normally we should reget the session.timeout value, if user postback the page.

    I suggest you could directly using below codes to reset the count after user accessed page again.

                  Session["Reset"] = true;
                    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
                    SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
                    int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);

    Best Regards,

    Brando

    Wednesday, September 19, 2018 6:44 AM
  • User-1171043462 posted

    This was an Article requirement. If you closely see, in my case I am doing redirect from JavaScript and hence I have added the condition.

    In your case, if it is not needed, it can be removed.

    Wednesday, September 19, 2018 3:00 PM
  • User1310055179 posted

    Thanks for your reply.

    I tried removing the condition as suggested, but I still don't get the expected results.

    It doesn't mattaer how many times I post back to the server,  I receive the alert message after 1 minute.

    Any idea what I'm doing wrong?

    I can see the following code in view source:

    <script type="text/javascript">
    //<![CDATA[
    SessionExpireAlert(60000);
    Thursday, September 20, 2018 6:32 AM
  • User1310055179 posted

    Ok, I think I figured out what is going on here.

    I added all the code to the master page and I expected the session to be reseted on each postback of the content page.

    I still don't understand why it's not working. I can see the masterpage code is suppose to run on each postback.

    Thursday, September 20, 2018 7:05 AM