1

I used Global.asax and this two codes give me error after session has timeout, like: 'Response is not available in this context.' - if I used Response.Redirect 'Object reference not set to an instance of an object.' - if I used HttpContext.Current.Response.Redirect

Global.asax

 protected void Session_End(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Redirect("timeout.aspx");
            Response.Redirect("~/timeout.aspx");
        }

and this is on my Web.Config

Web.Config

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors defaultRedirect="../error_page/" mode="Off"/>
    <sessionState timeout="1" cookieless="false" mode="InProc" ></sessionState>
  </system.web>
</configuration>

SessionTimeout.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {
            Session["CustomSessionId"] = Guid.NewGuid();
        }

Do you guys have any solutions, suggestion especially snippets to make this work out? I am just an entry level programmer, not an advance one.

Raniel Quirante
  • 315
  • 2
  • 15
  • Check this similar issue: https://stackoverflow.com/questions/484964/asp-net-push-redirect-on-session-timeout. Also check `Session.IsNewSession`, since `Session_End` event not attached to any `Request` instance. – Tetsuya Yamamoto Apr 03 '18 at 03:07

1 Answers1

1

HTTP is a stateless protocol that is initiated by the client. So you can't to use Session expiration to allow the page to automatically Redirect to the page, but you can try to use Js setInterval function set timeout time the value same as Sesssion.TimeOut to simulate.

So you can render a JS by DoRedirect method. The DoRedirect method require a parameter your Redirect page name.

window.setInterval require MilliSeconds time to set invoke time, so you need to You need to multiply 60000 by Session.Timeout.

SessionTimeout.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

}

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    DoRedirect("timeout.aspx");
}

public void DoRedirect(string page)
{
    int TimeOut = (this.Session.Timeout * 60000);
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.AppendLine("<script type='text/javascript'>");
    sb.AppendLine("window.setInterval('Redirect()'," +TimeOut.ToString() + @"); ");
    sb.AppendLine(" function Redirect(){ ");
    sb.AppendLine("window.location.href='/" + page + @"';");
    sb.AppendLine("}");
    sb.AppendLine(" </script>");

    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", sb.ToString());
}
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • Just to clarify, do I add this to global.asax? Because when I did add this, on asax, this error shows up. ERR_TOO_MANY_REDIRECTS – Raniel Quirante Apr 03 '18 at 03:34
  • Yes you need to add `Application_PostRequestHandlerExecute` on global.asax, I edit my anwser:) – D-Shih Apr 03 '18 at 05:18
  • there is still an existing problem, although it redirects to timeout.aspx and doesn't ERR_TOO_MANY_REDIRECTS anymore, it doesn't wait on sessionstate timeout to be timed out. – Raniel Quirante Apr 03 '18 at 06:09
  • To be precise, when I run the SessionTimeout.aspx, it redirects immediately on timeout.aspx – Raniel Quirante Apr 03 '18 at 06:10
  • I see,Why don't you Redirect to `timeout.aspx` on `SessionTimeout.aspx.cs` `Page_Load` directly, just like my edit2 answer – D-Shih Apr 03 '18 at 07:08
  • Still the same output, do you know a method where you can post on SessionTimeout.aspx without a button using session_end? – Raniel Quirante Apr 03 '18 at 07:16