C# MVC4 project: I want to redirect to a specific page when the session expires.
After some research, I added the following code to the Global.asax in m
This is some something new in MVC.
Public class SessionAuthorizeAttribute : AuthorizeAttribute
{
Protected override void HandleUnauthorizeRequest(
AuthorizationContext filtercontext )
{
filtercontext.Result = new RedirectResult("~/Login/Index");
}
}
After apply this filter on your controller on those where you want to apply authorization.
[SessionAuthorize]
public class HomeController : Controller
{
// Something awesome here.
}
Above SessionAuthorizeAttribute's HandleUnAuthorizeRequest function will only call once authorization is failed Instead of repeatedly check for authorization.
Regards MK