2010-08-25 5 views
10

Auschecken der Beispielcode von http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc für ASP.NET MVC2 geschrieben, bemerkte ich, dass sie überprüfen können, ob ein benutzerdefiniertes Attribut auf die aktuelle Aktion oder Controller angewendet wird Zugriff auf filterContext.ActionDescriptor und filterContext.ActionDescriptor.ControllerDescriptor jeweils:Get Liste der benutzerdefinierten Attribute für aktuelle Aktion/Controller in ASP.NET MVC

public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter { 
    public void OnAuthorization(AuthorizationContext filterContext) { 
     // snip 

     // abort if a [RequireHttps] attribute is applied to controller or action 
     if(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 
     if(filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 

     // snip 
    } 
} 

Was die ASP.NET MVC-1-Methode zur Überprüfung der Aktion und Controller für ein benutzerdefiniertes Attribut sein würde? In ASP.NET MVC 1 gibt es keine filterContext.ActionDescriptor, die ich sagen kann.

Antwort

6

Das scheint zu funktionieren ... gibt es einen besseren/geeigneteren Weg in ASP.NET MVC 1?

if (filterContext.Controller.GetType().GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) 
    return; 
string action = (string)filterContext.RouteData.Values["action"]; 
if (!string.IsNullOrEmpty(action) && filterContext.Controller.GetType().GetMethod(action).GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) 
    return; 
+0

ich wissen muss - was tun diese Rückkehr? Warum kommst du gerade zurück? – muttley91

+0

Sie können im ursprünglichen Code in der Frage sehen, dass dieses Snippet innerhalb der "public void OnAuthorization" -Methode eines Attributs ist. Wenn sich das RequireHttpsAttribute auf dem aktuellen Controller oder der aktuellen Aktion befindet, ist es am besten, die Methode zurückzugeben und so fortzufahren, als wäre nichts passiert. Wenn sich RequireHttps jedoch NICHT auf dem Controller oder der Aktion befindet, wird Code ausgeführt. In diesem Fall wird der Browser wahrscheinlich auf ein Nicht-HTTPS-Protokoll umgeleitet. – DavGarcia

+0

Beachten Sie, dass dieses Snippet für ASP.NET MVC 1 und möglicherweise MVC 2 war. Seit MVC 3 gibt es bessere Möglichkeiten, mit dem Problem der Überprüfung von Controller- und Aktionsfiltern umzugehen, wie Sunday Ironfoot herausstellte. – DavGarcia

18

Noch besser und zuverlässiger * Ansatz:

filterContext.ActionDescriptor.GetCustomAttributes(
    typeof(RequireHttpsAttribute), true).Count> 0 

Obwohl dies könnte MVC sein 3.0+ nur.

+3

Dies scheint nicht das Gleiche zu tun. Ich habe das mit einem Attribut auf dem Controller versucht und dies hat false zurückgegeben. Die Verwendung des Codes der ursprünglichen Frage funktioniert jedoch gut. –

+0

Mit beiden zusammen arbeiten sehr gut in MVC 4 'if (filterContext.Controller.GetType() GetCustomAttributes (typeof (SkipLocationFilterAttribute), true) .Any().) \t \t \t \t { \t \t \t \t \t Rückkehr; \t \t \t \t} \t \t \t \t if (filterContext.ActionDescriptor.GetCustomAttributes (typeof (SkipLocationFilterAttribute), false) .Any()) \t \t \t \t { \t \t \t \t \t return; \t \t \t \t} ' – PvtVandals

+0

@PvtVandals Ich war die Suche nach irgendwo ein vergoldeten Ausgabe von Ihnen zu setzen (I unabhängig abgeleitet), [tada] (http://stackoverflow.com/a/34956930/11635) –

11

Vergoldet Ausgabe, arbeitet auf MVC5, wahrscheinlich 4/3:

filterContext.HasMarkerAttribute<RequireHttpsAttribute>() 

Verwendet diesen Satz von Helfer-Erweiterungen:

public static class MarkerAttributeExtensions 
{ 
    public static bool HasMarkerAttribute<T>(this AuthorizationContext that) { 
     return that.Controller.HasMarkerAttribute<T>() 
      || that.ActionDescriptor.HasMarkerAttribute<T>(); 
    } 

    public static bool HasMarkerAttribute<T>(this ActionExecutingContext that) { 
     return that.Controller.HasMarkerAttribute<T>() 
      || that.ActionDescriptor.HasMarkerAttribute<T>(); 
    } 

    public static bool HasMarkerAttribute<T>(this ControllerBase that) { 
     return that.GetType().HasMarkerAttribute<T>(); 
    } 

    public static bool HasMarkerAttribute<T>(this Type that) { 
     return that.IsDefined(typeof(T), false); 
    } 

    public static IEnumerable<T> GetCustomAttributes<T>(this Type that) { 
     return that.GetCustomAttributes(typeof(T), false).Cast<T>(); 
    } 

    public static bool HasMarkerAttribute<T>(this ActionDescriptor that) { 
     return that.IsDefined(typeof(T), false); 
    } 

    public static IEnumerable<T> GetCustomAttributes<T>(this ActionDescriptor that) { 
     return that.GetCustomAttributes(typeof(T), false).Cast<T>(); 
    } 
} 
+1

Vielen Dank für poste dies! – agrath

+0

Für jeden, der dies liest, können Sie 'AuthorizationContext' in' ActionExecutingContext' ändern, damit diese Erweiterungsmethoden mit regulären 'ActionFilterAttribute's – elexis

+1

@elexis Guter Punkt funktionieren, danke !; editiert in. –