Finbuckle / Finbuckle.MultiTenant

Finbuckle.MultiTenant is an open-source multitenancy middleware library for .NET. It enables tenant resolution, per-tenant app behavior, and per-tenant data isolation.
https://www.finbuckle.com/multitenant
Apache License 2.0
1.26k stars 261 forks source link

Option to ignore specific routes and static files #225

Open DeeJayTC opened 4 years ago

DeeJayTC commented 4 years ago

When working with a SPA approach I have a few common endpoints used for everyone and per tenant endpoints using route strategy.

Is there any way to ignore the tenant check for specific routes or all static files?

AndrewTriesToCode commented 4 years ago

Hi @DeeJayTC

For files if you place UseStaticFiles before UseMultiTenant regular files should not be affected. However there currently isn't a built in way to prevent the multitenant middleware from running otherwise. You could create a custom MultiTenantStrategy to do that, but I think this is an excellent idea for a new feature.

DeeJayTC commented 4 years ago

Yea, especially when you use the dpa middleware its constantly looking up tenant from the hot module replacement checks , websocket connections etc, too.

Could you maybe, to make it easier add something that allows to say:

UseRouteStrategyExplicit( RoutesThatShouldCheckTenant )

Any other route is ignored?

Or else any route without the Tenant is ignored automatically per option?

DeeJayTC commented 4 years ago

SPA middleware...not dpa :)

hbermani commented 4 years ago

In a similar place here with a SPA application and it looks very chatty on the server end checking finbuckle tenancy unnecessarily. Will try and implement a custom strategy but will also greatly welcome the proposed enhancement.

AndrewTriesToCode commented 4 years ago

Thanks for the input!

It's funny because in ASP.NET Core 2.1 you actually can set a different route for the strategy but then in ASP.NET Core 2.2 they changed to "Endpoint" routing which overall is better but has this one issue.

In general the tenant strategy is fast but the tenant store check can be slow. So with that in mind I have a few ideas. Tell me what you think:

  1. Exception list - Have a way to tell the middleware to ignore certain identifiers if it finds them. E.g. if it thinks it finds a tenant identifier of 'spa_folder' then just treat it like a null and don't look into the store, etc.
  2. Add events to the middleware like with authentication so you can register a function for events like OnTenantIdentifierFound, check the identifier it found, and tell it to ignore it by setting a result to Handled or something like that.

I plan to do both, hopefully #1 in the next release.

hbermani commented 4 years ago

Thanks for the input!

It's funny because in ASP.NET Core 2.1 you actually can set a different route for the strategy but then in ASP.NET Core 2.2 they changed to "Endpoint" routing which overall is better but has this one issue.

In general the tenant strategy is fast but the tenant store check can be slow. So with that in mind I have a few ideas. Tell me what you think:

  1. Exception list - Have a way to tell the middleware to ignore certain identifiers if it finds them. E.g. if it thinks it finds a tenant identifier of 'spa_folder' then just treat it like a null and don't look into the store, etc.
  2. Add events to the middleware like with authentication so you can register a function for events like OnTenantIdentifierFound, check the identifier it found, and tell it to ignore it by setting a result to Handled or something like that.

I plan to do both, hopefully #1 in the next release.

In my case Finbuckle middleware is running even when the React app is being loaded. I think that's partly due to poor architecture my end. I was thinking I will separate the SPA to load from a different project where multitenancy is not required and then just run the backend APIs and persistence with multitenancy.

Update: Implemented the above and it now works better.

AndrewTriesToCode commented 4 years ago

@hbermani Excellent!