0

我有一个基于订阅的 MVC 2 应用程序,其中包含基本的 .NET Membership 服务(在一些自定义组件下方以管理帐户/订阅等)。帐户已失效或手动暂停其帐户的用户需要能够访问系统中管理其帐户状态的单个视图。使用 [Authorize] 属性保护驱动该视图的控制器。

我想确保在用户重新激活其帐户之前,无法访问系统中的其他视图。在我的基本控制器(我所有受保护的控制器都从中派生)中,我尝试修改 OnActionExecuting 方法以拦截操作,检查暂停的帐户,如果它被暂停,则重定向到管理帐户状态的单个视图。但这让我陷入了无限循环。当新动作被命中时, OnActionExecuting 再次被调用,并且循环继续进行。

我真的不想扩展 [Authorize] 属性,但如果需要可以。

关于如何在控制器级别执行此操作的任何其他想法?

编辑:在基本控制器中,我通过修改 filterContext.Result 属性来管理重定向(随后创建了重定向循环),将其设置为我有问题的视图的 RedirectToAction 结果。我注意到每次循环发生时,filterContext.Result == null。也许我应该检查 filterContext 的不同部分?

4

1 回答 1

0

好的,这是我的解决方案,以防它对其他人有所帮助。必须有一种更优雅的方式来做到这一点,如果有人有更好的主意,我会全力以赴。

在我的 BaseController.cs 中:

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ViewData["CurrentUser"] = CurrentUser; // this is a public property in the BaseController

        if (CurrentUser != null && CurrentUser.Account.Status != AccountStatus.Active)
        {
            // if the account is disabled and they are authenticated, we need to allow them
            // to get to the account settings screen where they can re-activate, as well as the logoff
            // action.  Everything else should be disabled.
            string[] actionWhiteList = new string[] { 
                Url.Action("Edit", "AccountSettings", new { id = CurrentUser.Account.Id, section = "billing" }), 
                Url.Action("Logoff", "Account")
            };

            var allowAccess = false;
            foreach (string url in actionWhiteList)
            {
                // compare each of the whitelisted paths to the raw url from the Request context.
                if (url == filterContext.HttpContext.Request.RawUrl)
                {
                    allowAccess = true;
                    break;
                }
            }

            if (!allowAccess)
            {
                filterContext.Result = RedirectToAction("Edit", "AccountSettings", new { id = CurrentUser.Account.Id, section = "billing" });
            }
        }

        base.OnActionExecuting(filterContext);
    }
于 2010-12-02T15:58:40.847 回答