0

基本的な.NETメンバーシップサービスが配置されたサブスクリプションベースのMVC 2アプリケーションがあります(アカウント/サブスクリプションなどを管理するためのカスタムコンポーネントの下)。アカウントが失効したユーザー、またはアカウントを手動で停止したユーザーは、アカウントのステータスを管理するシステムで単一のビューにアクセスできる必要があります。そのビューを駆動するコントローラーは、[Authorize] 属性を使用して保護されます。

ユーザーがアカウントを再度有効にするまで、システム内の他のビューにアクセスできないようにしたいと考えています。基本コントローラー (保護されているすべてのコントローラーが派生する) で、OnActionExecuting メソッドを変更して、アクションをインターセプトし、停止されたアカウントを確認し、停止されている場合は、アカウントの状態を管理する単一のビューにリダイレクトしようとしました。しかし、これは私を無限ループに陥らせます。新しいアクションがヒットすると、OnActionExecuting が再度呼び出され、サイクルが続行されます。

[Authorize] 属性を拡張したくはありませんが、必要に応じて拡張できます。

コントローラーレベルでこれを行う方法について他に何か考えはありますか?

EDIT:ベースコントローラーでは、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 に答える