I am exposing web API through ServicesApiController and want to authorize user through [AuthorizedUser(@"sitecore\ServicesAPI")] attribute of the function, everything is working fine, but instead of returning 401 status, Sitecore returning 404 status.
The solution I am thinking to terminate the request if authorization failed and return the request from the "OnAuthorization" method only or handle this scenario from 404 not found pipeline.
I am using this code for authorization
public override void OnAuthorization(HttpActionContext actionContext)
{
base.OnAuthorization(actionContext);
var context = Context.User;
//string test = context.ToString();
if ((context.IsAuthenticated && context.Name.Equals(_user)))
return;
actionContext.Response =
actionContext.ControllerContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
"Unauthorized Access; User is " + Context.User.LocalName);
base.OnAuthorization(actionContext);
}
and validate the controller function like below:
[AuthorizedUser(@"sitecore\ServicesAPI")]