I have an API developed in .NET7 with multiple controllers. Right now all the actions have the same authorization policy so we accomplished it by setting it up in the useendpoints middleware like below
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute().RequireAuthorization("MyPolicy")
});
But now we are trying to have separate policies for GET and POST's. Is there a way to dynamically achieve this with out decorating each method with a different Authorize("Policy") attribute. Thank you in Advance
A work around for you: create a route constraint:
configure as below in your program.cs:
another workaround:
create a middleware replace the default authorize middleware: