5

Ich habe einen Standard CustomAuthorizeAttribute in meinem Web-API-Projekt definiert. Autorisierungsfilter mit einem anderen überschreiben

config.Filters.Add(new CustomAuthorizeAttribute()); 

Allerdings habe ich einen speziellen Controller, wo ich eine SpecialAuthorizeAttribute verwenden möchten.

[SpecialAuthorize] 
public class MySpecialController : ApiController 

Im Asp.Net vNext haben wir ein neues Attribut die Standardfilter außer Kraft zu setzen, aber wie könnte ich es 2 im Web-Api funktioniert?

Edit 1:

Eine mögliche (aber nicht ideal) Lösung ist, die CustomAuthorizeAttribute Prüfung machen, wenn es eine andere AuthorizeAttribute im Rahmen des Controllers oder Aktion ist. In meinem Fall habe ich die SpecialAuthorizeAttribute nur so:

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) 
    { 
     if (actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<SpecialAuthorizeAttribute>().Any() || actionContext.ActionDescriptor.GetCustomAttributes<SpecialAuthorizeAttribute>().Any()) 
     { 
      return; 
     } 
     base.OnAuthorization(actionContext); 
    } 

    public override System.Threading.Tasks.Task OnAuthorizationAsync(System.Web.Http.Controllers.HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken) 
    { 
     return base.OnAuthorizationAsync(actionContext, cancellationToken); 
    } 
} 
+1

Sie könnten Ihr CustomAuthorizeAttribute auf die anderen Controller anwenden, anstatt es global zu machen, indem Sie es zur Konfiguration hinzufügen. Es bedeutet, dass Sie jeden Controller dekorieren müssen, aber es definiert wahrscheinlich genauer, was Sie tatsächlich tun. –

Antwort