2010-12-19 11 views
0

"SomeController"というコントローラがあります。私は、ユーザーがログインしているかどうか、またはそのコントローラで何らかのアクションを実行しようとしているのかどうかをチェックしたいと思います。そうするために、私はその記事http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/を読んで、私は自分のクラス(テスト)を書いた:ASP.NET MVC 2 OnActionExecutingメソッドの問題

public class BaseFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
     { 
      FormsAuthentication.RedirectToLoginPage(); 
     } 
     //here will be checking the user permissions if he's logged in 
    } 
} 

[BaseFilter] 
public class SomeController : BaseController 
{ 
... 
} 

をしかし、あなたが理解できるように私は、そのコントローラから任意のアクションを実行したいときには、不定詞のループを作ります。では、それにどのように対処するのですか?

答えて

1

アクションフィルタは、クラスレベルではなく関連するメソッドに適用できます。

私は個人的にはAuthorizeのような名前を付けて、承認が必要なコントローラメソッドに適用します。

[Authorize] 
public ActionResult Index() 
{ 
// Do stuff 
} 
関連する問題