Finbuckle.MultiTenant icon indicating copy to clipboard operation
Finbuckle.MultiTenant copied to clipboard

Tenant availability validation

Open niravbhattsai opened this issue 5 years ago • 4 comments

I am using route strategy with in memory store.

I have added 2 tenants in the store tenant1 and tenant2 along with its connection string and all.

Now while opening page http://localhost/tenant1/Home/Index or http://localhost/tenant2/Home/Index. It is working fine as expected.

However, if I open http://localhost/tenant3/Home/Index, it is also opening however while database query it is failing as the tenant is not valid.

I have tried with the route strategy sample provided in the source, the issue is there also.

Am I missing something?

I would like to achieve that if the tenant is not available in the system should show 404 not found.

image

niravbhattsai avatar Mar 25 '20 16:03 niravbhattsai

Hi @niravbhattsai

You aren't missing anything, in that case the request is being routed to the controller with no tenant detected, and in my view I decided to show the default page if no tenant was detected.

You could do something in your controller like this if you wanted to return a 404:

if(HttpContext.GetMultiTenantContext()?.TenantInfo == null)
    return NotFound();

Maybe it would make for a good enhancement to somehow automatically return a 404 if the tenant isn't found when using the RouteStrategy.

AndrewTriesToCode avatar Mar 25 '20 21:03 AndrewTriesToCode

Thanks @achandlerwhite, I am planning to use the similar approach.

I agree with you, it will be a good enhancement to return a 404. I did not get chance to look at the host strategy, but I believe it will be similar output.

In Host, if we map a CNAME record as * and in IIS we map *.domain.com, it is behaving same as route strategy.

niravbhattsai avatar Mar 29 '20 10:03 niravbhattsai

@niravbhattsai I've pinned this issue as an enhancement so I can come back to it later. I'm glad you found a workaround for now-- let me know if you run into any problems.

AndrewTriesToCode avatar Mar 30 '20 17:03 AndrewTriesToCode

Using this code everywhere can bore you. it looks better like this.

public class ActionFilterExample : ActionFilterAttribute
   {
       public override void OnActionExecuting(ActionExecutingContext context)
       {
           if (context.HttpContext.GetMultiTenantContext<TenantInfo>()?.TenantInfo == null)
               context.Result = new BadRequestResult();
       }
   }

And

    [ActionFilterExample]
    public class HomeController : Controller

dev-fatih-erol avatar Aug 29 '22 06:08 dev-fatih-erol