17

ASP.NET MVC2 用に記述されたhttp://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvcのサンプル コードを調べたところ、カスタム属性が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

これはMVC3.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 に答える
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 に答える