Redirect after Session Timeout in MVC Action Filter

馋奶兔 提交于 2021-01-29 08:02:18

问题


I have to redirect to home page after session timeout in mvc 5 application. I am using following code :

 public class SessionExpireAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            var currentEnvironment = EnvironmentUtility.GetSession().Get<Environment>();
            if (currentEnvironment == null)
            {
                filterContext.Result = new RedirectResult("~/Home/Index");
            }
            base.OnActionExecuting(filterContext);
        }

    }

In the controller, I have below code :

[SessionExpire]
public ActionResult Submit()
{
    var urlReferrer = this.Request.UrlReferrer;
    if (urlReferrer != null)
    {
     return this.Redirect(urlReferrer.ToString());
    }
    else
    {
        return View();
    }
}

After the submit button is clicked, the page is redirecting to another page using javascript code as like below :

 function commitButtonClickedHandler()
{
       window.location.href = "@Url.Content("~/Working/NextPage")";
}

And the NextPage is like below :

public ActionResult NextPage()
        {
            return this.View();
        }

What my requirement is after session timeout, when you click the submit button it should check seesion expiration. If it is expired , then it should be redirected to NextPage as like in SessionExpireAttribute. Else it should continue the process .But problem is after session timeout, the page is still redirecting to NextPage() action . Kindly help me to fix this. Thank you.


回答1:


You can Do it by overriding OnActionExecuting making some change as below:

public class SessionExpireAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {            
            if ( Session["SomeSession"] == null)                    
            {
                //ClearAllSessions
                GoToLoginPage(filterContext, "You Session has been expired");
                return;
            }           
    }

    private static void GoToLoginPage(ActionExecutingContext filterContext, string message)
    {
        try
        {           


          filterContext.Result = new RedirectToRouteResult(
                 new RouteValueDictionary 
            { 
                { "controller", "controllerName" }, 
                { "action", "actionName" } ,
                { "Area","" },
                { "UnauthLogin",message },

            });
            }
        }
        catch
        {
            filterContext.Result = new RedirectToRouteResult(
                   new RouteValueDictionary 
            { 
                { "controller", "controllerName" }, 
                { "action", "actionName" } ,
                { "Area","" },
                { "UnauthLogin",message },

            });
        }
    }
 }


来源:https://stackoverflow.com/questions/37803908/redirect-after-session-timeout-in-mvc-action-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!