-
Notifications
You must be signed in to change notification settings - Fork 162
Multi tenant configuration
The multi-tenant feature requires two configuration settings to make it work:
- Setting the AuthP's options.
- Defining the database arrangement. By default it uses one database, but if
AddSharding
is added, then it handles multiple databases. - Providing a implementation of the
ITenantChangeService
.
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.
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
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.
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.
- Intro to multi-tenants (ASP.NET video)
- Articles in date order:
- 0. Improved Roles/Permissions
- 1. Setting up the database
- 2. Admin: adding users and tenants
- 3. Versioning your app
- 4. Hierarchical multi-tenant
- 5. Advanced technique with claims
- 6. Sharding multi-tenant setup
- 7. Three ways to add new users
- 8. The design of the sharding data
- 9. Down for maintenance article
- 10: Three ways to refresh claims
- 11. Features of Multilingual service
- 12. Custom databases - Part1
- Videos (old)
- Authentication explained
- Permissions explained
- Roles explained
- AuthUser explained
- Multi tenant explained
- Sharding explained
- How AuthP handles sharding
- How AuthP handles errors
- Languages & cultures explained
- JWT Token refresh explained
- Setup Permissions
- Setup Authentication
- Startup code
- Setup the custom database feature
- JWT Token configuration
- Multi tenant configuration
- Using Permissions
- Using JWT Tokens
- Creating a multi-tenant app
- Supporting multiple languages
- Unit Test your AuthP app