Skip to content

Multi tenant configuration

Jon P Smith edited this page Apr 25, 2022 · 12 revisions

The multi-tenant feature requires two configuration settings to make it work:

  1. Setting the AuthP's options.
  2. Defining the database arrangement. By default it uses one database, but if AddSharding is added, then it handles multiple databases.
  3. Providing a implementation of the ITenantChangeService.

1. Setting the AuthP's multi-tenant options

Setting the AuthP's options allow you to define the structure of the data and the database arrangement you want to use - see Multi tenant explained for the definition of those two terms. The most important option is the TenantType property - this defines the structure and the database arrangement and must be set to turn on AuthP's multi-tenant features.

Defining the structure of the tenant data

The TenantTypes enum has two flag members that sets structure of the multi-tenant data.

  • TenantTypes.SingleLevel where each tenant is completely separate from other tenants.
  • TenantTypes.HierarchicalTenant where each tenant can create sub-tenants.

Here is an example of setting the TenantType to a hierarchical multi-tenant structure.

builder.Services.RegisterAuthPermissions<Example3Permissions>(options =>
    {
        options.TenantType = TenantTypes.HierarchicalTenant;
        //other options left out
    })
    //rest of AuthP registering left out

2. Defining the database arrangement

The TenantTypes enum has a flag member called AddSharding which if added to the TenantType switches the multi-tenant database from using one database to using many databases defined in the application's appsetting file. It has to be matched with either a SingleLevel or HierarchicalTenant.

Also, if you turn on sharding with the AddSharding member you also need to set the option's Configuration property. The sharding feature needs to access the "ConnectionStrings" object in the appsettings file and the shardingsettings.json file. To do this it needs access ASP.NET Core's Configuration class.

Here is an example of setting the TenantType to a single-level multi-tenant structure which uses multiple databases.

builder.Services.RegisterAuthPermissions<Example3Permissions>(options =>
    {
        options.TenantType = TenantTypes.SingleLevel | TenantTypes.AddSharding;
        options.Configuration = builder.Configuration;
        //other options left out
    })
    //rest of AuthP registering left out

NOTE: See the Sharding database settings page for how this works.

3. Registering your tenant change service

When you want to create, update, delete etc. a tenant the AuthP library provides the code / services to update the tenant's admin information, but its very likely that something should change something in the tenant database. Therefore, if you are using any of multi-tenant features you must create a class that follows the ITenantChangeService and register that class to using the RegisterTenantChangeService<T> register method.

This example taken from Example3 shows the use of the RegisterTenantChangeService<T> method.

builder.Services.RegisterAuthPermissions<Example3Permissions>(options =>
    {
        options.TenantType = TenantTypes.SingleLevel;
        //other options left out
    })
    .UsingEfCoreSqlServer(connectionString)
    .RegisterTenantChangeService<InvoiceTenantChangeService>()
    //rest of AuthP registering left out

The writing of a tenant change service is quite complex and is covered in the Building a tenant change service.

Additional resources

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally