There is always a way :)
Let's say you have an area - Area55 - with an api controller - AlienController -
Create your custom Controller Model Convention so that any controller under Area55 will be authorized by apipolicy
public class MyAuthorizeFiltersControllerConvention : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
string apiArea;
if (controller.RouteValues.Any()
&& controller.RouteValues.TryGetValue("area", out apiArea)
&& apiArea.Equals("Area55"))
{
controller.Filters.Add(new AuthorizeFilter("apipolicy"));
}
else
{
controller.Filters.Add(new AuthorizeFilter("defaultpolicy"));
}
}
}
Then register it in startup.cs
services.AddMvc(o =>
{
o.Conventions.Add(new MyAuthorizeFiltersControllerConvention());
});
still in your startup.cs, configure the policies to use the authentication scheme that we want
services.AddAuthorization(o =>
{
o.AddPolicy("defaultpolicy", b =>
{
b.RequireAuthenticatedUser();
b.AuthenticationSchemes = new List<string> { IISDefaults.AuthenticationScheme };
});
o.AddPolicy("apipolicy", b =>
{
b.RequireAuthenticatedUser();
b.AuthenticationSchemes = new List<string> { JwtBearerDefaults.AuthenticationScheme };
});
});
The controller might be something like this
[Area("Area55")]
[Route("[area]/api/[controller]")]
[ApiController]
public class AlienController : ControllerBase
{
}
That's all I guess.