I am inheriting from System.Web.Http.AuthorizeAttribute to create a custom authorization/authentication routine to meet some unusual requirements for a web
To add to the already accepted answer: Checking current sourcecode (aspnetwebstack.codeplex.com) for System.Web.Http.AuthorizeAttribute
, it looks like the documentation is out of date. Base OnAuthorization()
just calls/checks private static SkipAuthorization()
(which just checks if AllowAnonymousAttribute
is used in context to bypass the rest of the authentication check). Then, if not skipped, OnAuthorization()
calls public IsAuthorized()
and if that call fails, it then calls protected virtual HandleUnauthorizedRequest()
. And that's all it does...
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext == null)
{
throw Error.ArgumentNull("actionContext");
}
if (SkipAuthorization(actionContext))
{
return;
}
if (!IsAuthorized(actionContext))
{
HandleUnauthorizedRequest(actionContext);
}
}
Looking inside IsAuthorized()
, that's where Principle is checked against roles and users. So, overriding IsAuthorized()
with what you have above instead of OnAuthorization()
would be the way to go. Then again, you'd still have to probably override either OnAuthorization()
or HandleUnauthorizedRequest()
anyway to decide when to return a 401 vs a 403 response.