The easiest way in MVC is that
In case of Session Expire, in every action you have to check its session and if it is null then redirect to Index page.
For this purpose you can make a custom attribute as shown :-
Here is the Class which overrides ActionFilterAttribute.
public class SessionExpireAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
// check sessions here
if( HttpContext.Current.Session["username"] == null )
{
filterContext.Result = new RedirectResult("~/Home/Index");
return;
}
base.OnActionExecuting(filterContext);
}
}
Then in action just add this attribute as shown :
[SessionExpire]
public ActionResult Index()
{
return Index();
}
Or Just add attribute only one time as :
[SessionExpire]
public class HomeController : Controller
{
public ActionResult Index()
{
return Index();
}
}
Session_End
event doesn't happen in the context of a current request - it's an even that is sent to the application outside of the normal run time - therefore you cannot issue a redirect request. Typically this event is used to allow the application to clean-up resources from the just ended session. It would be hard to have it happen during a request as one of the first things a request does is activate the session. – Faun