✅ Put `[Authorize("myPolicy")]` behind feature flag
Greetings,
I'm currently working on authentication and authorization for an API that is already deployed to a couple of customers. The whole auth process sits behind a feature flag using the MS
IFeatureManager
, so that the API can be deployed completely without auth and once more downtime is possible from customers side auth shall be activated by enabling this feature.
I have pretty much all covered by the feature flag, but I don't know how I can conditionally apply the [Authorize("myPolicy")]
on my api endpoints.
If the feature is disabled I don't even register any auth related services or middlewares.7 Replies
you can't conditionally apply an attribute, as they are compile time metadata
but if as you say the feature is disabled there are no auth middlewares, doesnt it still work?
Unfortunately it doesn't work, as I don't even register any services and middleware if the feature is disabled. I get an exception that the auth metadata is defined but no
app.UseAuthorization()
call is made.Ah, thats unfortunate.
The recommended approach seems to be to make your policies conditional
ie,
replace that if with your feature toggle check
replacing all your policies with "blank" policies
That doesn't work either unfortunately, I get
AuthorizationPolicy must have at least one requirement.
there you go 🙂
Thanks! It works! I had to get a custom
IAuthorizationPolicyProvider
alongside your solution to support Authorize
attributes with policies that I don't have defined when the feature is disabled. Works like a charm now👍