解决自定义AuthorizeAttribute实现授权管理,AllowAnonymous属性失效导致无法匿名访问控制器的问题

一开始出现问题,在网上找各种资料,后来看到了这篇文章,http://blog.csdn.net/song_jiang_long/article/details/52605660,按照文章里讲的,这么去做,就会提示找不到IsDefined,后来去看官方的代码,终于解决了,附上解决方案

在重写的OnAuthorization里加上如下代码,

            bool flag = ((ReflectedHttpActionDescriptor)actionContext.ActionDescriptor).MethodInfo.IsDefined(typeof(AllowAnonymousAttribute), true) ||
                actionContext.ActionDescriptor.ControllerDescriptor.ControllerType.IsDefined(typeof(AllowAnonymousAttribute), true);
            if (flag)
            {
                return;
            }

AllowAnonymous属性就可以用了

原文地址:https://www.cnblogs.com/varorbc/p/8084512.html