Redirect to specific page after session expires (MVC4)

后端 未结 3 1038
花落未央
花落未央 2020-12-05 08:26

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

3条回答
  •  没有蜡笔的小新
    2020-12-05 08:44

    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

提交回复
热议问题