{"id":85,"date":"2009-03-27T08:54:00","date_gmt":"2009-03-27T08:54:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2009\/03\/27\/catch-the-session-expired-case\/"},"modified":"2009-03-27T08:54:00","modified_gmt":"2009-03-27T08:54:00","slug":"catch-the-session-expired-case","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2009\/03\/27\/catch-the-session-expired-case\/","title":{"rendered":"Catch the Session Expired case"},"content":{"rendered":"<p>The project I am working on requires to show a nice custom error page to the user when the session is expired. I searched a bit on the internet and came up with a good solution in <a href=\"http:\/\/forums.asp.net\/t\/1282128.aspx\">forums.asp.net<\/a> which I worked it out as follows:<\/p>\n<div><\/div>\n<div>As mentioned in the forum you need to implement an\u00a0IHttpModule\u00a0and redirect the context&#8217;e\u00a0AcquireRequestState\u00a0event to your method which goes this:<\/div>\n<div><span   style=\"  white-space: pre; font-family:-webkit-monospace;font-size:13px;\">        <\/span><\/div>\n<div>\n<pre>        <span style=\" ;color:#0000FF;\">void<\/span><span style=\" ;color:#000000;\"> Application_AcquireRequestState(<\/span><span style=\" ;color:#0000FF;\">object<\/span><span style=\" ;color:#000000;\"> sender, EventArgs e)<br \/>      {<br \/>          <\/span><span style=\" ;color:#0000FF;\">if<\/span><span style=\" ;color:#000000;\"> (HttpContext.Current.Session <\/span><span style=\" ;color:#000000;\">!=<\/span><span style=\" ;color:#000000;\"> <\/span><span style=\" ;color:#0000FF;\">null<\/span><span style=\" ;color:#000000;\"> <\/span><span style=\" ;color:#000000;\">&amp;&amp;<\/span><span style=\" ;color:#000000;\"> HttpContext.Current.Session.IsNewSession)<br \/>          {<br \/>              <\/span><span style=\" ;color:#0000FF;\">if<\/span><span style=\" ;color:#000000;\"> (HttpContext.Current.Request.Cookies <\/span><span style=\" ;color:#000000;\">!=<\/span><span style=\" ;color:#000000;\"> <\/span><span style=\" ;color:#0000FF;\">null<\/span><span style=\" ;color:#000000;\"> <\/span><span style=\" ;color:#000000;\">&amp;&amp;<\/span><span style=\" ;color:#000000;\"> HttpContext.Current.Request.Cookies[<\/span><span style=\" ;color:#000000;\">\"<\/span><span style=\" ;color:#008080;\">ASP.NET_SessionId<\/span><span style=\" ;color:#000000;\">\"<\/span><span style=\" ;color:#000000;\">] <\/span><span style=\" ;color:#000000;\">!=<\/span><span style=\" ;color:#000000;\"> <\/span><span style=\" ;color:#0000FF;\">null<\/span><span style=\" ;color:#000000;\">)<br \/>              {<br \/>                  HttpContext.Current.Session.Clear();<br \/>                  HttpContext.Current.Response.Redirect(<\/span><span style=\" ;color:#000000;\">\"<\/span><span style=\" ;color:#008080;\">\/Errors\/SessionTimeout.aspx<\/span><span style=\" ;color:#000000;\">\"<\/span><span style=\" ;color:#000000;\">, <\/span><span style=\" ;color:#0000FF;\">true<\/span><span style=\" ;color:#000000;\">);<br \/>              }<br \/>          }<br \/>      }<br \/>    <br \/>  }<br \/><br \/><\/span><\/pre>\n<p><\/div>\n<p>As you can see I clear the session before redirecting to the SessionTimeout.aspx. The reason for that is that I want the user have a choice of going to login screen by providing a link in the SessionTimeout.aspx. And the login requires a session state unlike the\u00a0SessionTimeout.aspx that has the attribute <code>EnableSessionState=\"False\"\u00a0<\/code><\/p>\n<div>\n<p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The project I am working on requires to show a nice custom error page to the user when the session is expired. I searched a bit on the internet and came up with a good solution in forums.asp.net which I worked it out as follows: As mentioned in the forum you need to implement an\u00a0IHttpModule\u00a0and &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2009\/03\/27\/catch-the-session-expired-case\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Catch the Session Expired case&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"The project I am working on requires to show a nice custom error page to the user when the session is expired. I searched a bit on the internet and came up with a good solution in forums.asp.net which I worked it out as follows: As mentioned in the forum you need to implement an\u00a0IHttpModule\u00a0and&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/85"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}