WebAPI中controller添加[AllowAnonymous]無效的解決方法


 對於Methods添加[AllowAnonymous]可以進行匿名訪問,但是對於Controller添加時無效

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

  }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM