I'm struggling with authentication in Web Api project that use OData. I thought that configuring authentication like this:
services.AddODataAuthorization((options) =>
{
options.ConfigureAuthentication(JwtBearerDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApi(Configuration.GetSection(Constants.AzureAd));
});
will be enough to enable authentication on all requests to OData endpoints. But no matter if token is passed, valid or invalid - api methods are always executed. HTTP 401 (unauthorized) is never returned.
In one of the examples there is an explicit use of HasReadRestrictions method:
customers.HasReadRestrictions()
.HasPermissions(p => p.HasSchemeName("Scheme").HasScopes(s => s.HasScope("Customers.Read")))
.HasReadByKeyRestrictions(r => r.HasPermissions(p =>
p.HasSchemeName("Scheme").HasScopes(s => s.HasScope("Customers.ReadByKey"))));
Is it mandatory to configure all OData entities like that?
[Authorize]attribute on your controllers or methods?