WebAPI中controller添加[AllowAnonymous]无效的解决方法

 对于Methods添加[AllowAnonymous]能够进行匿名访问,可是对于Controller添加时无效ide

public class AuthAttribute : AuthorizationFilterAttribute
{
  public override void OnAuthorization(HttpActionContext actionContext)
  {
    //若是用户方位的Action带有AllowAnonymousAttribute,则不进行受权验证,可是controller中无效
    //if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())
    //{
    // return;
    //}
  
    //分别验证在method和controller中的AllowAnonymousAttribute属性
    if (((ReflectedHttpActionDescriptor)actionContext.ActionDescriptor).MethodInfo.IsDefined(typeof(AllowAnonymousAttribute), true)
      ||actionContext.ActionDescriptor.ControllerDescriptor.ControllerType.IsDefined(typeof(AllowAnonymousAttribute), true))     
    {
      return;
    }
        
    //token验证
    ...

  }
}

 
相关文章
相关标签/搜索