I'm implemented a [CustomAuthorization]
attribute based on [Authorize]
attribute. My attribute looks like this:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public eUserRole CustomRoles { get; set; } = eUserRole.Administrator; // If not specified, the required role is Administrator
protected override bool IsAuthorized(HttpActionContext actionContext)
{
AuthorizationSystem auth = new AuthorizationSystem(actionContext.RequestContext.Principal, this.CopyleaksRoles);
var res = auth.Validate();
if (!res)
return false;
return base.IsAuthorized(actionContext);
}
}
I splitted the logic (who to accept and who not) to seperated class. The method AuthorizationSystem.Validate()
return true if the user is accepted according to his CustomRoles
property.
My Controller looks like:
[CustomAuthorize]
public class MyController : ApiController
{
[CustomAuthorize(CustomRoles = eUserRole.Readonly)]
public Response Do()
{
// ... Do something ...
}
}
I'm running the application (C# + WebAPI) to check if it working.
I debugging the code and see that on the first run the minimum required role level is Administrator
instead of Readonly
. Because when using [CustomAuthorize]
without any CustomRoles
, it's define the default row to be eUserRole.Administrator
. That mean that the first CustomAuthorize
attribute that being called is the attribute on class level, not on method level.
How to make it call the attribute that on the method (Do()
) before?