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驗證
    ...

  }
}

 
相關文章
相關標籤/搜索