Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: The original question was about how to detect session logout, and then automatically redirect to a specified controller and action. The question proved much more useful as it's current form however.</p> <hr> <p>I ended up using a combination of items to achieve this goal.</p> <p>First is the session expire filter found <a href="http://www.tyronedavisjr.com/index.php/2008/11/23/detecting-session-timeouts-using-a-aspnet-mvc-action-filter/" rel="noreferrer">here</a>. Then I wanted someway to specify the controller/action combo to get a redirect URL, which I found plenty of examples of <a href="http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx" rel="noreferrer">here</a>. In the end I came up with this:</p> <pre><code>public class SessionExpireFilterAttribute : ActionFilterAttribute { public String RedirectController { get; set; } public String RedirectAction { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpContext ctx = HttpContext.Current; if (ctx.Session != null) { if (ctx.Session.IsNewSession) { string sessionCookie = ctx.Request.Headers["Cookie"]; if ((null != sessionCookie) &amp;&amp; (sessionCookie.IndexOf("ASP.NET_SessionId") &gt;= 0)) { UrlHelper helper = new UrlHelper(filterContext.RequestContext); String url = helper.Action(this.RedirectAction, this.RedirectController); ctx.Response.Redirect(url); } } } base.OnActionExecuting(filterContext); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload