Currently I have the following configurations for a asp.net core 2.1 project in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//... some other configs
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
var authorizeFilter = new AuthorizeFilter(policy);
options.Filters.Add(authorizeFilter);
});
services.AddAuthorization(options =>
{
options.AddPolicy("PolicyA", builder =>
builder.Requirements.Add(new RequirementA()));
options.AddPolicy("PolicyB", builder =>
builder.Requirements.Add(new RequirementB()));
});
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "authority";
options.Audience = "audience";
});
//... some more configs
}
and also a Controller that starts like this:
[Authorize(Policy="PolicyA"]
[Authorize(Policy="PolicyB"]
public class MyController : ControllerBase
{
// super awesome code I can't show :P
}
My goal is to execute the AuthorizeFilter that handles the basic Bearer authentication and authorization before "PolicyA" and "PolicyB". At the moment it seams like the AuthorizeFilter is executed last after "PolicyA" and "PolicyB".
I already looked into similar issues posted here, but I only found solutions for setting the execution order of Attributes, but I want to use the MVC filter if possible instead of an attribute for the basic authentication and authorization.
So this one unfortunately didn't help me: Action filter execution order
Update: As Xerillio suggested in the comments that policies are probably meant to be self-contained, I adapted my AuthorizationHandlers to also check what my AuthorizeFilter is checking already. This does not feel like the best possible solution, but I will keep it for now.