Handling missing tenants in ASP.NET Core

My last post about multi-tenants applications with ASP.NET Core Implementing database per tenant strategy on ASP.NET Core I proposed some ideas about tenant providers but I didn’t focused on issues related to missing tenants. This post proposes missing tenant middleware that redirects faulty requests to some public page of company web site.

Ideas for handling missing tenants

I expect here that tenants are served by ASP.NET Core web application that works always in context of some tenant. I mean there will be no public content pages with sales or marketing materials and so on. The web application needs some tenant to be available. But what happens when tenant is not found by host header?

One approach is to throw an exception but this is hardly an option with commercial service. Redirect to some public page would be better but making redirects in controllers means duplicated code. It’s possible to build some TenantRequired attribute but I don’t like this idea either.

Building missing tenant middleware

As a working solution I see custom middleware that is added to request processing pipeline. This middleware takes redirect URL as argument and checks if tenant is detected or not. If there’s no tenant available it redirects request to redirect URL.

public class MissingTenantMiddleware
{
    private readonly RequestDelegate _next;
    private readonly string _missingTenantUrl;

    public MissingTenantMiddleware(RequestDelegate next, string missingTenantUrl)
    {
        _next = next;
        _missingTenantUrl = missingTenantUrl;
    }

    public async Task Invoke(HttpContext httpContext, ITenantProvider provider)
    {
        if(provider.GetTenant() == null)
        {
            httpContext.Response.Redirect(_missingTenantUrl);
            return;
        }

        await _next.Invoke(httpContext);
    }
}

Notice how redirect URL is given to middleware through constructor injection and tenant provider is argument of Invoke() method call. It was the minimal I was able to achieve without tricking in Startup class of application.

Adding missing tenant middleware to request pipeline

Middleware must be registered during application start-up. Here is the fragment of Configure() method of Startup class.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // ...

    app.UseMiddleware<MissingTenantMiddleware>(Configuration["MissingTenantUrl"]);

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Although the case of missing tenant is now handled in middleware it doesn’t mean that other classes that expect existence of current tenant should not control if tenant is available or not.

Wrapping up

Without any hacking but leaning to ASP.NET Core features it is possible to handle many unexpected situations in web applications. This blog post focused on multi-tenant applications and a situation where request is made but tenant is not found. Using custom middleware it was possible to build missing tenant middleware that redirects those requests to some public page given in application settings. All the logic is located in missing tenant middleware and no other parts of web application have to take care about redirects in case of missing tenant.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    3 thoughts on “Handling missing tenants in ASP.NET Core

    • September 24, 2018 at 12:36 pm
      Permalink

      I tried to redirect the page to the home page of the same site. Then I’m getting the error too many redirects (Infinite redirects.
      httpContext.Response.Redirect(_missingTenantUrl);

      Any solution to handle this?

    • January 15, 2020 at 11:40 am
      Permalink

      It’s not working , I’m getting the error too many redirects.

    • January 18, 2020 at 1:13 pm
      Permalink

      Too many redirects comes from the fact that you are trying to redirect user to some page of multi-tenant application and this page is not part of any tenant. Same time web application expects there to be active tenant. It can’t find it and redirects user again to _missingTenantUrl.

      In practice, multi-tenant application is one thing and homepage of service provider is another thing. So, _missingTenantUrl should redirect user to landing page that is part of company’s homepage.

      It’s possible to update missing tenant provider the way that it ignores request to _missingTenantUrl. In this case you must be sure that controller action for this URL doesn’t need any multi-tenant features. Otherwise you will get NullReferenceExceptions and other annoyiances.

    Leave a Reply

    Your email address will not be published. Required fields are marked *