Dynamically enable/disable a route in ASP.NET 7?
Is there a way to dynamically enable/disable a route in ASP.NET 7? I'd like to be able to enable/disable a registration endpoint of an API depending on whether or not an admin selects.
11 Replies
You can probably create a custom action filter and evaluate whatever condition in there
And then handle it accordingly
Besides that, probably something like configuring a route is not very dynamic, as well as disabling by attribute
you can map the action filter to a feature flag system 🤔
I currently have JWT authentication setup, so once a user registers and then logs in they get a bearer token to make API calls to protected endpoints
for example, users have to provide a token to POST to a certain endpoint
but the issue is, they can still access the register endpoint to create an account, if I make that authorized only, then someone who wants to enable public signups can't
what ?
just don't show tu signup button if they are connected
I want to be able to have a toggle for an admin to enable or disable new users signups, and if its disabled then I don't want anyone except admins being able to make a call to the registration endpoint
create a system to do feature flag
like a database table for example
with a ServiceFilter attribute on top of your controller
and in the filter check if the flag "createUser" is enabled or whatever and f it's not check if the user role
Filters in ASP.NET Core
Learn how filters work and how to use them in ASP.NET Core.
there is example for servicefilter
I'd also separate this into 2 endpoints, since one is a registration and the other is create, which may prove useful to have as 2 separate flows (which can just call the same service for now)
Then you can feature flag toggle just the register endpoint but leave the admin create endpoint always on and put role filter on it.
What do you mean registration and create?
I'm not sure how else to word it
I'd split your logic into 2 distinct api endpoints, 1 for admins to create users amd 1 for users to self register
And just feature flag the latter