AspNetCoreOperationSecurityScopeProcessor does not work for global authorization policies
See original GitHub issueI want to use a global authorization filter, but keep the option to exclude certain controllers / methods with the [AllowAnonymous]
attribute.
The generated open api document should add the the security requirement for all methods, except the ones with the [AllowAnonymous]
attribute.
AspNetCoreOperationSecurityScopeProcessor
works fine when using [Authorize]
/ [AllowAnonymous]
attributes on controllers and methods, but it will not recognize global authorization policy filters:
services.AddControllers(options =>
{
options.Filters.Add(
new AuthorizeFilter(
new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
});
or .net core 3:
app.UseEndpoints(endpoints =>
{
endpoints
.MapControllers()
.RequireAuthorization();
});
Using OperationSecurityScopeProcessor
instead will add the security requirement for all methods in the open api document, regardless of any additional [AllowAnonymous]
attributes on controllers / methods and therefore is not really a good option.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
NSwag's AspNetCoreOperationSecurityScopeProcessor ...
I've decorated actions in my controllers with [AllowAnonymous] and [Authorize(AuthenticationSchemes = "ClientApp")] , however NSwag marks all of ...
Read more >Setting global authorization policies using the ...
In this post I show multiple ways to configure global authorization policies, and look at the difference between the DefaultPolicy and the ...
Read more >Policy-based authorization in ASP.NET Core
Learn how to create and use authorization policy handlers for enforcing authorization requirements in an ASP.NET Core app.
Read more >Custom Authorization Policy Providers in ASP.NET Core
Learn how to use a custom IAuthorizationPolicyProvider in an ASP.NET Core app to dynamically generate authorization policies.
Read more >Enabling multiple authorization methods in NSwag : r/dotnet
Hey, I'm looking for a way to enable both JWT Bearer authorization and a simple API-KEY method in the nswag UI.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Working on a proof of concept, I faced the same issue as mentioned above.
Situation:
Not backwards compatible (so used a separate class name) and untested, but maybe the following implementation might help forward. Not sure how this will behave in a solution where also Authorize-attributes with Role are specified.
If you look at the code then you see that only roles are taken into account: https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs
Can you create a PR to improve that with the right behavior (and hopefully not breaking changes)?