locked
Multi Tenancy - best approach RRS feed

  • Question

  • User269881539 posted

    I want to run a site as a multi-tenant set up - using the host header as the differentiator - I need this to drive some content on every page.

    So if the site is accessed by a particular URL I need logos / titles etc to react to that - elements that will be in my _Layout.cshtml mostly. I will have to refer to the value for some aspects of actual functionality but it is mostly a presentational issue.

    What would be the most efficient method of doing this site wide - it will affect every request - trying to minimise having to add code to every controller / round trips to the database etc.

    Many Thanks

    Wednesday, August 12, 2020 2:39 PM

All replies

  • User-474980206 posted

    It’s just like localization , except instead of language Id  it’s tenant id. You need a database of strings indexed by tenant Id and string id 

    Wednesday, August 12, 2020 2:46 PM
  • User269881539 posted

    Thanks - I was thinking of how / where to store the value - so when the user comes onto the site we detect the current host, and I can do a lookup in a table to grab some site settings - site name / logo file etc

    It was where to call this code (some sort of session start?) and where to store the info (session also?) so that I don't have to call the same code in every controller to go off and determine the site, and fetch the values from the db each request?

    Wednesday, August 12, 2020 2:50 PM
  • User-474980206 posted

    session will not work unless you also store tenant id, and verify. if the user hits another tenant they will still have the same session.

    i'd use a scoped DI object that had the data. be sure it uses HttpContext DI to get the host header. You could also add a cache.  

    Wednesday, August 12, 2020 3:57 PM
  • User269881539 posted

    So effectively checking on each request / in each controller? Get the Host Header value, cross ref to my db for the other values? Can I create something that is called by default for (before) each request to evaluate it rather than polluting every controller with the code?

    Wednesday, August 12, 2020 4:48 PM
  • User475983607 posted

    So effectively checking on each request / in each controller? Get the Host Header value, cross ref to my db for the other values? Can I create something that is called by default for (before) each request to evaluate it rather than polluting every controller with the code?

    That's what middleware is for in ASP.NET Core.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1

    Wednesday, August 12, 2020 4:50 PM