【问题标题】:Check Session timeout on invoking constructor of a controller in MVC 4在 MVC 4 中调用控制器的构造函数时检查会话超时
【发布时间】:2014-11-17 18:52:16
【问题描述】:

我正在使用以下方法在我的 MVC 4 应用程序中处理会话到期:

第 1 步:创建以下类:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

         string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
         string actonName = filterContext.ActionDescriptor.ActionName.ToLower();
         if (!(controllerName.Contains("account")||(controllerName.Contains("home") && actonName.Contains("index"))))
         {

             // If the browser session or authentication session has expired...
             if (SessionManager.Instance["PlatformId"] == null || !filterContext.HttpContext.Request.IsAuthenticated)
             {
                 if (filterContext.HttpContext.Request.IsAjaxRequest())
                 {

                     filterContext.Result = new JsonResult { Data = "_Logon_" };
                 }
                 else
                 {

                     filterContext.Result = new RedirectToRouteResult(
                         new RouteValueDictionary {
                    { "Controller", "Home" },
                    { "Action", "TimeoutRedirect" }
            });
                 }
             }
         }
        base.OnActionExecuting(filterContext);
    }
}

第 2 步:将其添加到 RegisterGlobalFilters,如下所示:

    filters.Add(new SessionExpireFilterAttribute());

我在会话处于活动状态并且工作正常时对此进行了测试 - 在执行每个操作时它正在检查会话是否处于活动状态。但问题是我正在使用会话值在构造函数中初始化一些对象,如下所示:

     public class DashboardController : BaseController
{
    private DashboardService dashboardService;


    public DashboardController()
    {

        dashboardService = new DashboardService(this.DbContext, (int)SessionManager.Instance["PlatformId"]);

    }
 }

当会话超时时,显然会抛出空引用异常

        dashboardService = new DashboardService(this.DbContext, (int)SessionManager.Instance["PlatformId"])

在进行会话到期检查之前。我不能将所有这些初始化移动到每个动作,因为它很忙——我已经有很多动作方法。

那么有没有办法在调用构造方法时检查会话超时?请帮忙。

【问题讨论】:

    标签: c# asp.net-mvc-4 session


    【解决方案1】:

    嗯,

    我正在做的是在应用程序的 Application_AcquireRequestState 方法 (Global.asax) 中检查会话

    类似的东西

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
         if (HttpContext.Current.Handler is IRequiresSessionState)
         {
             var usrobj = HttpContext.Current.Session["User"];
             if(usrobj == null)
             {
                 Response.Redirect("~/Login/Index");
             }
         }
    }
    

    我不知道你是否想要类似的东西。

    【讨论】:

    • 感谢您的回复。有没有办法知道在这个 Application_AcquireRequestState 方法中调用了哪个控制器和操作方法?
    • 你不能在 Global.asax 中调用控制器或任何动作方法,你想用它来实现什么?
    • 我不想从 global.asax 中调用控制器或操作方法,我只想检查正在调用哪个操作,因为仅针对帐户控制器中的索引以外的操作我想检查会话到期.
    • 我相信您可以通过以下方式检查正在调用哪个控制器/操作: string path = System.Web.HttpContext.Current.Request.Path.ToString();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多