C#C
C#2y ago
nox7

✅ ASP Core Attribute Logic Help

I need assistance with structuring my controller attribute logic (they're authorization attributes) in ASP Core 2 - I am using .NET 8.

I have the following objects (for this purpose): ClientAccount, Dashboard, and StripeAccount.

When a client accesses a route, I need to
  • Verify they are logged in
  • Verify they are accessing a Dashboard that exists
  • Verify they are accessing a Dashboard they have authorization to view
  • Verify, for some routes, the Dashboard has a connected StripeAccount
My idea was to add these attributes to the controllers
[RequireClientAccount]
[RequireDashboard]
[RequireClientHasAccessToDashboard]


Because some routes don't need the 3rd one. How can I approach this without each route having to run duplicate database calls? As in, RequireDashboard and RequireClientHasAccessToDashboard would both need to re-fetch the current Dashboard from the HttpContext since routes don't have any set order they run in - so they can't rely on one already having fetched the Dashboard object? Would a scoped object with static properties work to store if an object has already been fetched?

Because attributes have no defined order, I am duplicating Db queries in each attribute to perform the same operation at the moment.
Was this page helpful?