AllowAnonymous not working with Custom AuthorizationAttribute

后端 未结 8 637
一整个雨季
一整个雨季 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

    public class MyAuthorizationAuthorize : AuthorizeAttribute, IAuthorizationFilter
    {
    public override void OnAuthorization(AuthorizationContext filterContext)
            {
                if (filterContext.HttpContext.Request.IsAuthenticated)
                {
                    bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
                        filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
    
                    if (skipAuthorization) return;
    
                }
                else filterContext.Result = new HttpUnauthorizedResult();
            }
    }
    

提交回复
热议问题