AllowAnonymous not working with Custom AuthorizationAttribute

后端 未结 8 652
一整个雨季
一整个雨季 2020-12-03 06:34

This has had me stumped for a while. None of the commonly encountered similar situations seem to apply here apparently. I\'ve probably missed something obvious but I can\'

8条回答
  •  一个人的身影
    2020-12-03 06:56

    In the AuthorizeAttribute there is the following code:

    private static bool SkipAuthorization(HttpActionContext actionContext)
    {
        Contract.Assert(actionContext != null);
    
        return actionContext.ActionDescriptor.GetCustomAttributes().Any()
                   || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes().Any();
    }
    

    Include this method in your AuthorizeAttribute class then add the following to the top of your OnAuthorization method to skip authorization if any AllowAnonymous attributes are found:

    if (SkipAuthorization(actionContext)) return;
    

提交回复
热议问题