C
C#•7mo ago
Vortac

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
Sossenbinder
Sossenbinder•7mo ago
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
x0rld
x0rld•7mo ago
you can map the action filter to a feature flag system 🤔
Vortac
Vortac•7mo ago
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
x0rld
x0rld•7mo ago
what ? just don't show tu signup button if they are connected
Vortac
Vortac•7mo ago
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
x0rld
x0rld•7mo ago
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
x0rld
x0rld•7mo ago
there is example for servicefilter
PixxelKick
PixxelKick•7mo ago
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.
Vortac
Vortac•7mo ago
What do you mean registration and create?
PixxelKick
PixxelKick•7mo ago
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