locked
Session Timout and Refresh Metatag RRS feed

  • Question

  • User1111434469 posted

    Hello,

    I'd like my page to refresh through MetaTag and redirect to SessionEnd.aspx, when the user Session expires. I use this code:

            protected void Page_Load(object sender, EventArgs e)
            {
                HtmlMeta meta = new HtmlMeta();
                meta.HttpEquiv = "Refresh";
                meta.Content = ((Session.Timeout + 1) * 30).ToString() + ";url=/SessionEnd.aspx";
                this.Page.Header.Controls.Add(meta);    
            }

    If SessionTimout = 1 minute, this code attaches Refresh Metatag, but it refreshes after 2 minutes, not 1 minute after the Session expires. If the user browse the page, the Session doesn't expires. The session should expires 1 minute after the user stop browsing the page.

    Is it possible to control the real session behaviour, not just to put static value, like 2 minutes or SessionTimout + custom minutes.

    Can I use something like:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (this.Page.Header.Controls.Contains(meta))
                {
                    this.Page.Header.Controls.Remove(meta);
                }
            }

    and everytime reset this Metatag Refresh minutes?

     


     

     

     

    Thursday, February 10, 2011 2:55 PM

Answers

  • User-660870441 posted

    Yes, the code redirected to SessionEnd.aspx, but it if you start browsing (clicking links) in that test page, the page will be redirected after 2 minutes too. The session won't be expired but the page will be redirected. This is not the goal. The goal is when session expired, the page to redirect. When user browse the page, the session.timeout should expand, the redirecting of page in this example doesn't expand the time, the time is static, and the page will redirect in the middle of clicking..

    Hi,

    If you only open one page at one time, the refresh meta tag should work. When the browser requests a page, the session timeout will reset (expand), and the meta tag will reset too. That is to say session timeout and the refresh meta tag are synchronous. So it is working well in this case.

    But at one time, if you open a page in a tab (the browser tab) and then open another page in another tab by click a link or something else, session timeout will be synchronous with refresh meta tag in the new page. In this case, refer meta tag in the first page is no longer synchronous with session timeout. So the first page will redirect even though session timeout reset actually.

    If this is the case, refresh meta tag is not suitable anymore. I would suggest you just do the redirection in server side.

    if (Session["userName"] == null)
    {
        Response.Redirect("~/SessionEnd.aspx");
    }

    As far as I know, it is hard to achieve redirection automatically in this case.

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 14, 2011 2:24 AM
  • User1111434469 posted

    Hi there,

    Thanks for all your help on the topic. I've just dealt with the problem with ASP.NET AJAX control ModalPopup and Timer_Tick event. When I want the user to be informed something from my site in certain time (1 minute) when he stopped sending requests, I put:

    <asp:Timer ID="Timer1" runat="server" Interval="60000" ontick="Timer1_Tick">
    
    


     

            protected void Timer1_Tick(object sender, EventArgs e)
            {
                showModalPopupServerOperatorButton_Click(null, null);
    }


     

    and my session.timout = 1; which expires some seconds after that.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 14, 2011 2:05 PM

All replies

  • User533502567 posted

    If SessionTimout = 1 minute, this code attaches Refresh Metatag, but it refreshes after 2 minutes, not 1 minute after the Session expires.
     

    How about this..?

    Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) ) & "; URL=SessionEnd.aspx");  

    If the user browse the page, the Session doesn't expires.

    The session Timeout is a sliding expiration time, meaning whever your page access session state, the expiration time will be moved forward.  Please note that as long as a page has NOT disabled session state, it will access the session automatically when requested.

    Friday, February 11, 2011 10:25 AM
  • User1111434469 posted

    If SessionTimout = 1 minute, this code attaches Refresh Metatag, but it refreshes after 2 minutes, not 1 minute after the Session expires.
     

    How about this..?

    1. Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) ) & "; URL=SessionEnd.aspx");    
    Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) ) & "; URL=SessionEnd.aspx");  

     

    This is the same what I did, but with another syntax.

     

    The session Timeout is a sliding expiration time, meaning whever your page access session state, the expiration time will be moved forward.  Please note that as long as a page has NOT disabled session state, it will access the session automatically when requested.

    Yes, that's right, session is a sliding expiration time. Are there any metatags which can offer sliding expiration time like session, or is it possible to add and remove Refresh metatag on each Page_Load.

    Something like that i'm thinking about...

    Also, recently i have another idea:

    On some event (maybe Timer_Tick event) write some code like checking the Session.Timout property and if it's about to expire to call Response.Redirect("SesssionEnd");. Let's say, if I want my session to be 5 minutes, and I estimate that user is not touching the page for 5 minutes, i put in web.config SessionTimeout = 6 and on 5 minutes I call Response.Redirect("SesssionEnd");

    Friday, February 11, 2011 11:56 AM
  • User533502567 posted

    This is the same what I did, but with another syntax.

    When i tested posted code, page redirected to sessionend.aspx at the end of timeout value.

    Yes, that's right, session is a sliding expiration time. Are there any metatags which can offer sliding expiration time like session, or is it possible to add and remove Refresh metatag on each Page_Load.
     

    Why do you want to remove those refresh meta tags? Whenever page loads, session timeout will reset to intial value. So you have to add those meta tags on each page_load. Remember previously added meta tags are gone.

    Friday, February 11, 2011 12:11 PM
  • User1111434469 posted

    Yes, the code redirected to SessionEnd.aspx, but it if you start browsing (clicking links) in that test page, the page will be redirected after 2 minutes too. The session won't be expired but the page will be redirected. This is not the goal. The goal is when session expired, the page to redirect. When user browse the page, the session.timeout should expand, the redirecting of page in this example doesn't expand the time, the time is static, and the page will redirect in the middle of clicking..

    Friday, February 11, 2011 2:11 PM
  • User-660870441 posted

    Yes, the code redirected to SessionEnd.aspx, but it if you start browsing (clicking links) in that test page, the page will be redirected after 2 minutes too. The session won't be expired but the page will be redirected. This is not the goal. The goal is when session expired, the page to redirect. When user browse the page, the session.timeout should expand, the redirecting of page in this example doesn't expand the time, the time is static, and the page will redirect in the middle of clicking..

    Hi,

    If you only open one page at one time, the refresh meta tag should work. When the browser requests a page, the session timeout will reset (expand), and the meta tag will reset too. That is to say session timeout and the refresh meta tag are synchronous. So it is working well in this case.

    But at one time, if you open a page in a tab (the browser tab) and then open another page in another tab by click a link or something else, session timeout will be synchronous with refresh meta tag in the new page. In this case, refer meta tag in the first page is no longer synchronous with session timeout. So the first page will redirect even though session timeout reset actually.

    If this is the case, refresh meta tag is not suitable anymore. I would suggest you just do the redirection in server side.

    if (Session["userName"] == null)
    {
        Response.Redirect("~/SessionEnd.aspx");
    }

    As far as I know, it is hard to achieve redirection automatically in this case.

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 14, 2011 2:24 AM
  • User1111434469 posted

    Hi there,

    Thanks for all your help on the topic. I've just dealt with the problem with ASP.NET AJAX control ModalPopup and Timer_Tick event. When I want the user to be informed something from my site in certain time (1 minute) when he stopped sending requests, I put:

    <asp:Timer ID="Timer1" runat="server" Interval="60000" ontick="Timer1_Tick">
    
    


     

            protected void Timer1_Tick(object sender, EventArgs e)
            {
                showModalPopupServerOperatorButton_Click(null, null);
    }


     

    and my session.timout = 1; which expires some seconds after that.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 14, 2011 2:05 PM