AllowAnonymous not working with Custom AuthorizationAttribute

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

    Using MVC 5
    Steps to overcome this issue:-
    1. Update your Anonymous attribute of WebAPI project and make it like

    [System.Web.Mvc.AllowAnonymous]
    
    1. Now go to your custom attribute class and write the code

       public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext filterContext)
      {
          if (filterContext == null)
          {
              throw new UnauthorizedAccessException("Access Token Required");
          }
          base.OnAuthorization(filterContext);
          if (filterContext.ActionDescriptor.GetCustomAttributes().Any())
          {
              return;
          }
          if (filterContext.Request.Headers.Authorization != null)
          {
              var response = 
       PTPRestClient.GetRequest(filterContext.Request.Headers.Authorization.ToString(), 
       "api/validate/validate-request");
              if (!response.IsSuccessStatusCode)
              {
                  throw new UnauthorizedAccessException();
              }
      
      
          }
          else
          {
              throw new UnauthorizedAccessException("Access Token Required");
          }
      }
      

提交回复
热议问题