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.31k stars 267 forks source link

Error 404 NotFound with Pathbase Strategies #637

Open HDScarpe opened 1 year ago

HDScarpe commented 1 year ago

Hello. I use Finbuckle.MultiTenant library with Duende. Currently I am using PathBase Strategy to define tenants. I have successfully identified the tenant on login. However after successful login i call the controller swagger then i have 2 problems and question 1: Can I use ClaimStrategies to identify tenants after login. Currently I cannot use ClaimStrategies to determine after successful login even though I call it in AddMultiTenant like the following code

 return services
             .AddDbContext<TenantDbContext>(options => options
                 .UseSqlServer(connectionString))
             .AddMultiTenant<ScarpeTenant>()
             .WithBasePathStrategy(
                 ctx =>
                 {
                     ctx.RebaseAspNetCorePathBase = true;
                 }) 
             .WithClaimStrategy("tenantid")
             .WithHeaderStrategy(SystemConstants.TenantIdName)
             .WithEFCoreStore<TenantDbContext, ScarpeTenant>()
             .WithPerTenantAuthentication()
             .Services; 

2: Since ClaimStrategies couldn't be used on its own in issue 1. So I added the tenant parameter at the top of the api (as shown in figure). However, after entering the correct tenant "root" Got a 404 NotFound error and could not call the controller. I don't understand what it means to get a 404 NotFound. If I reluctantly enter a non-existent tenant as "root1", the controller successfully calls back.

  1. entering the correct tenant "root" image

  2. enter a non-existent tenant as "root1" image

Here is all my code . You can view and reply to me. Thank you.

AndrewTriesToCode commented 1 year ago

Hi, for the Claims strategy you have to make sure IdentityServer (and internally ASP.NET Core Identity) are setting a tenant claim on the principle it generates. I'm snot sure from you code if you have it doing that or not.

With regard to BasePath strategy -- Swagger won't show the base path because it won't even see it. If the multitenant middleware detects the tenant from the base path it will adjust the base path (if you are using this option) so that subsequent middleware will only see the remaining path -- so your route parameter there wouldn't see any tenant. From your API's perspective it should define routes as if the tenant base path isn't there. Any calls to the URI will reality have the tenant base path so when the API is called it's active tenant should be correct. I hope that makes sense.