12

セッション値に基づいてコントローラーアクションへのアクセスを承認する新しいアクションフィルター([承認]と同様の属性)を作成しました。ただし、基本的にすべてのコントローラーアクションをその属性で装飾しています(ごく一部を除く)。

したがって、 [ExemptFromAuthorize]属性をコントローラーアクションにアタッチする場合を除いて、アクションフィルターを常に実行する方がよいと思いました。(たぶん、自分のControllerクラスに継承することで?)

これどうやってするの?

4

7 に答える 7

7

jeef3の答えで走って、私はこれを思いついた。複数の区切られたアクションのように、より多くのエラーチェックと堅牢性を使用できますが、一般的な考え方は機能します。

特定のケースでは、セッション値をテストして、承認から戻ることも決定できます。

public class AuthorizeWithExemptionsAttribute : AuthorizeAttribute
{
    public string Exemption { get; set; }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.RouteData.GetRequiredString("action") == Exemption)
            return;

        base.OnAuthorization(filterContext);
    }

}

使用法:

[AuthorizeWithExemptions(Roles="admin", ExemptAction="Index")]
public class AdminController : Controller
...
于 2009-08-27T21:48:13.093 に答える
7

codeprojectに関する私の記事をチェックしてください-

http://www.codeproject.com/KB/web-security/AuthorizeWithExemptions.aspx

この記事では、ASP.NET MVCアプリケーションのコントローラーをセキュリティで保護するためのソリューションを提供します。これにより、セキュリティで保護されていないと定義したアクションを除くすべてのアクションがセキュリティで保護されます。

コードからのスニッパー:

public override void OnAuthorization(AuthorizationContext filterContext)
{
    ActionDescriptor action = filterContext.ActionDescriptor;
    bool IsUnsecured = action.GetCustomAttributes(
                         typeof(UnsecuredActionAttribute), true).Count() > 0;

    //If doesn't have UnsecuredActionAttribute - then do the authorization
    filterContext.HttpContext.SkipAuthorization = IsUnsecured;

    base.OnAuthorization(filterContext);
}
于 2010-08-27T17:04:45.460 に答える
6

質問はかなり時代遅れですが、とにかく..すべてのアクションにフィルターを適用したい場合は、Global.asaxに次の行を追加してください。

protected void Application_Start()
{
    // your code here and then
    RegisterGlobalFilters(GlobalFilters.Filters);
}    

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new MyActionFilterAttribute());
}

また、アクションフィルタでは、次の方法でアクションに他の属性があるかどうかを確認できます。

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.ActionDescriptor.IsDefined(typeof(AnotherActionAttribute), false))
    {
        // do what you want to do
    }
}
于 2013-05-17T09:48:58.210 に答える
3

たぶんExcept、最初の属性にプロパティを追加してみてください。

[MyAuthenticate(Exempt="View")]
public class MyController : Controller
{
    public ActionResult Edit()
    {
        // Protected
    }

    public ActionResult View()
    {
        // Accessible by all
    }
}
于 2009-08-27T21:03:32.120 に答える
2

クラスに属性を追加して、そのクラスのすべてのメソッドに適用させることができます

[Authenticate]
public class AccountController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

クラスレベルの属性から特定のメソッドを除外する方法がわかりません。たぶん、認証されていないリクエストに別のコントローラーを使用しますか?

于 2009-08-27T20:56:59.060 に答える
2

2013年以降にこれを読んでいる人のために、MVC4は[AllowAnonymous]の使用をサポートするようになりました

コントローラに[承認]を設定してから、承認したくない機能に[匿名を許可]を設定できます。

例:

[承認]publicclass HomeController:Controller {

[AllowAnonymous]
public ActionResult Index()
{

} 

}

これはカスタムの[MyAuthorize]フィルターで機能しますか、それとも[Authorize]でのみ機能しますか?

于 2013-11-04T20:59:57.770 に答える
1

2013年以降にこれを読んでいる人のために、MVC4は現在 [AllowAnonymous]

コントローラに[承認]を設定してから、承認したくない機能に[匿名を許可]を設定できます。

例:

[Authorize]
public class HomeController : Controller
{

    [AllowAnonymous]
    public ActionResult Index()
    {

    }
}
于 2013-03-28T21:01:01.500 に答える