代码之家  ›  专栏  ›  技术社区  ›  Alex

对所有控制器操作(c/asp.net mvc)强制操作筛选器

  •  10
  • Alex  · 技术社区  · 16 年前

    我创建了一个新的操作过滤器(属性,类似于[授权]),它基于会话值授权对控制器操作的访问。但是,我基本上用这个属性来修饰我的所有控制器操作(除了极少数)。

    所以,我想最好有一个动作过滤器 总是 执行 除了 在我将[exemptfromauthorize]属性附加到控制器操作的情况下?(可能通过继承到我自己的控制器类?)

    我该怎么做?

    7 回复  |  直到 11 年前
        1
  •  6
  •   Community CDub    8 年前

    一起跑步 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
    ...
    
        2
  •  7
  •   Maxim    15 年前

    查看我关于代码项目的文章-

    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);
    }
    
        3
  •  6
  •   Andrew    12 年前

    我知道这个问题已经过时了,但无论如何……如果要对所有操作应用筛选器,只需在global.asax中添加以下行:

    protected void Application_Start()
    {
        // your code here and then
        RegisterGlobalFilters(GlobalFilters.Filters);
    }    
    
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new MyActionFilterAttribute());
    }
    

    在action过滤器中,您可以通过以下方式检查action是否具有任何其他属性:

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionDescriptor.IsDefined(typeof(AnotherActionAttribute), false))
        {
            // do what you want to do
        }
    }
    
        4
  •  3
  •   jeef3    16 年前

    可能尝试添加一个 Except 属性设置为第一个属性?

    [MyAuthenticate(Exempt="View")]
    public class MyController : Controller
    {
        public ActionResult Edit()
        {
            // Protected
        }
    
        public ActionResult View()
        {
            // Accessible by all
        }
    }
    
        5
  •  2
  •   John Sheehan    16 年前

    可以将属性添加到类中,使其应用于该类中的所有方法。

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

    我不知道如何从类级属性中排除特定的方法。可能对未经身份验证的请求使用单独的控制器?

        6
  •  1
  •   lanierhall    11 年前

    对于任何在2013年读到这篇文章的人来说,MVC4现在支持使用 [异基因]

    您可以在控制器上设置授权,然后允许匿名启用 您不想授权的任何函数。

    例子:

    [授权] 公共类主控制器:控制器{

    [AllowAnonymous]
    public ActionResult Index()
    {
    
    } 
    

    }

    这是否可以使用自定义的[MyAuthorize]筛选器,或者它只可以使用[Authorize]

        7
  •  0
  •   Kyle    12 年前

    对于任何在2013年读到这篇文章的人来说,MVC4现在支持使用 [AllowAnonymous]

    您可以在控制器上设置authorize,然后允许对不想授权的任何函数使用匿名。

    例子:

    [Authorize]
    public class HomeController : Controller
    {
    
        [AllowAnonymous]
        public ActionResult Index()
        {
    
        }
    }