17

查看http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc为 ASP.NET MVC2 编写的示例代码,我注意到他们可以检查自定义属性是否为通过分别访问filterContext.ActionDescriptor和应用于当前动作或控制器filterContext.ActionDescriptor.ControllerDescriptor

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
    }
}

检查自定义属性的动作和控制器的 ASP.NET MVC 1 方法是什么?在 ASP.NET MVC 1 中filterContext.ActionDescriptor,我无法判断。

4

5 回答 5

21

更好、更可靠*的方法:

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

虽然这可能只是 MVC 3.0+。

于 2012-12-02T10:48:22.107 回答
16

镀金版,适用于 MVC5,大概 4/3:

filterContext.HasMarkerAttribute<RequireHttpsAttribute>()

使用这组辅助扩展:

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>();
    }
}
于 2016-01-22T22:23:21.897 回答
11

这似乎有效......在 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;
于 2010-08-25T19:23:29.633 回答
3

这在 .NET Core 2.2 中对我有用:

var controllerActionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;

if (controllerActionDescriptor != null)
{
    // Check if the attribute exists on the action method
    if (controllerActionDescriptor.MethodInfo?.GetCustomAttributes(inherit: true)?.Any(a => a.GetType().Equals(typeof(CustomAttribute))) ?? false)
        return true;

    // Check if the attribute exists on the controller
    if (controllerActionDescriptor.ControllerTypeInfo?.GetCustomAttributes(typeof(CustomAttribute), true)?.Any() ?? false)
        return true;
}
于 2019-11-22T23:09:22.523 回答
1

我正在使用 MVC5 并且必须使用以下内容从继承自 ActionFilterAttribute 并实现 IAuthenticationFilter 的类中进行检查。

If filterContext.ActionDescriptor.GetCustomAttributes(GetType(RequireHttpsAttribute), True).Any() OrElse filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(GetType(RequireHttpsAttribute), True).Any() Then
' .. the given attribute is present ..
End If

我无法让 Ruben 的解决方案为我工作,但这可能是因为我在从 C# 到 VB 的转换中搞砸了。

于 2019-05-31T17:43:06.320 回答