Skip to content

Endpoint routing ignores [Authorize] attribute #695

Closed
@nikolai-mb

Description

@nikolai-mb

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:

  • Microsoft.AspNetCore.Authentication.JwtBearer - 5.0.0
  • Microsoft.AspNetCore.OData - 7.5.1
  • Microsoft.AspNetCore.OData.Versioning.ApiExplorer - 5.0.0
  • Swashbuckle.AspNetCore - 5.6.3

Authentication configuration is as follows in Startup.cs where "fakeauthority" is provided to force a 401 response.

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
    o.Authority = "https://fakeauthority.com";
});

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

services.AddMvc(options => options.EnableEndpointRouting = false);

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:

private static readonly bool _useEndpointRouting = false;

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions