-
Notifications
You must be signed in to change notification settings - Fork 712
Endpoint routing ignores [Authorize] attribute #695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Strange. I'm kind of curious how this even compiles. The package doesn't support the |
Thanks for the quick reply, i suppose you raise a good question as to why it compiles if the packages do not support net5.0. The repro i uploaded can be tested by changing targetframework to netcoreapp3.1 and downgrading the package Let me know if you are unable to reproduce and i'll try and provide more details |
I met the same problem in my project, targeting. net core 3.1. [Authorize] works correctly with none-odata endpoints in the same project. so it can't be a configuration mistake. |
@commonsensesoftware @nikolai-mb It seems that this issue is casused by EndpointMetadata missing. It happens in this method when rebuilding ActionDescriptor.. |
Using an attribute class inherited from AuthorizeAttribute and IFilterMetadata instead of [Authorize] attribute can be a workaround.
|
Bumping this, Thanks for the workaround @anranruye! |
@anranruye @nikolai-mb
Then, in the extension method
It might make sense, as well, to make the extension method generic, taking the attribute type as a type parameter, so this same workaround will help with multiple missing attribute types. Hope it helps. |
I made @manuel-fernandez-rodriguez method to accept a list of attribute types and put into EndpointBuilder metadata all attribute instances.
And the extension method and the class having it:
|
@adrianaxente - You are A M A Z I N G. Thanks for this; it worked perfectly well for me too. Relieve to get a fix for this. |
I am on dotnet-6 using Microsoft.AspNetCore.OData (7.5.12) and tried all the above approach and it is not working for me. Attribute wrapping approach worked for me with dotnet-5 though. Appreciate your suggestions |
@vikneshrajspui OData is not officially supported on .NET 5.0 or 6.0 - yet. OData 8.0 brought significant breaking changes to the infrastructure that is essentially a rewrite. I've sketched out a rough implementation and finally ready to start some active development. If you had things working in .NET 5.0 or earlier, my recommendation is to stay there - for now. |
Hello
I'm experiencing a strange issue where the
[Authorize]
attribute is not honored if endpoint routing is enabled togheter with odata api versioning. If endpoint routing is enabled, odata routes can be queried regardless if the request is authenticated or not.I'm currently testing in .NET 5, but issue is also reproducible in .NET Core 3.1.
.NET 5 reproduction is done with the following dependencies:
Authentication configuration is as follows in Startup.cs where "fakeauthority" is provided to force a 401 response.
Expected behaviour
curl http://localhost:51389/api/v1/users
curl : The remote server returned an error: (401) Unauthorized.
This works as expected with endpoint routing disabled
Actual behaviour with endpoint routing enabled
curl http://localhost:51389/api/v1/users
StatusCode : 200 StatusDescription : OK Content : {"@odata.context":"http://localhost:51389/api/v1/$metadata#Users","value":[{"id":1,"firstname":"Bob ","lastname":"Marley"},{"id":2,"firstname":"Mahatma","lastname":"Gandhi"}]} RawContent : HTTP/1.1 200 OK
I have uploaded a tiny project where issue can be reproduced:
https://github.com/nikolai-mb/ODataBugRepro
In the Startup.cs file of the repo above, the following variable exists:
When value is set to false, controller returns 401 as expected, but change variable to true and the issue appears.
I have not been able to reproduce with OData alone. Only with OData together with api-versioning and creating the OData route with
MapVersionedODataRoute
Any help would be much appricated
The text was updated successfully, but these errors were encountered: