Kephas.Tenants
Provides support for multi-tenant architecture. Typically used areas and classes/interfaces/services: Kephas Framework ("stone" in aramaic) aims to deliver a solid infrastructure for applications and application ecosystems.
Install
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard |
README
Tenants
Introduction
This package adds support for multi-tenant applications.
How it works:
- Include the
Kephas.Tenantspackage in the application deployment. - The application receives the tenant identifier through the
tenantcommand line argument or theKEPHASAPP_Tenantenvironment variable. - The tenant is available in code through
IAppArgs.Tenant()extension method or theIContext.Tenant()extension method.- Note that
IAppArgsare available through dependency injection andIContextinstances through the parameter received in context dependent invocations.
- Note that
- When setting up the
IAmbientServicesduring application bootstrapping, make sure to invoke theambientServices.WithTenantSupoport().- Do this typically before setting up the application runtime (for example
WithStaticAppRuntime,WithDynamicAppRuntime, orWithPluginsAppRuntime), to make sure the runtime accesses the correct locations.
- Do this typically before setting up the application runtime (for example
What is the immediate effect:
- Tenant administrative mode
- When the application was started without a tenant identifier specified either in command line arguments or in the environment variables. This mode should allow tenant management actions.
- The folder locations are not changed.
- Tenant specific mode
- The tenant identifier is passed to the running application.
- The folder locations (config, licenses, plugins) are found in the
{tenant}subdirectory of the original folder locations.
Check the following packages for more information:
Example
public static IAmbientServices SetupAmbientServices(
this IAmbientServices ambientServices,
Func<IAmbientServices, IEncryptionService> encryptionServiceFactory,
IConfiguration? configuration,
IAppArgs appArgs)
{
return ambientServices
.WithDefaultLicensingManager(encryptionServiceFactory(ambientServices))
// Do not forget setting up the tenant support right *after* setting up the application runtime.
.WithTenantSupport(appArgs)
//
.WithDynamicAppRuntime()
.WithSerilogManager(configuration);
}
Security
Tenants management
A global scope tenantsmanagement permission is provided to require in endpoints or other application actions affecting overall tenants management.
Tenant admin
A global scope tenantadmin permission is provided to require in endpoints or other application actions affecting management of the tenant.
Example
/// <summary>
/// Endpoint message for adding a tenant.
/// </summary>
[RequiresPermission(typeof(TenantsManagementPermission))]
public class AddTenantMessage : IMessage
{
//...
}
/// <summary>
/// Endpoint message for changing the tenant information.
/// </summary>
[RequiresPermission(typeof(TenantAdminPermission))]
public class ChangeTenantInfoMessage : IMessage
{
//...
}
Configuration
Tenant specific
The TenantSettings are available through the injectable IConfiguration<TenantSettings> service. It provides:
- DisplayName (string): The tenant's display name.
- Domains (string[]): an array of domain owned by the tenant. These domains are typically used in the authorization process to identify the tenant a user belongs to.
Example
var config = injector.Resolve<IConfiguration<TenantSettings>>();
var settings = config.GetSettings(context);
settings.DisplayName = "My changed name";
await config.UpdateSettingsAsync(settings, context).PreserveThreadingContext();
Tenant management
The TenantsManagementSettings are available through the injectable IConfiguration<TenantsManagementSettings> service. It provides:
- GlobalAdmin (GlobalAdminSettings): settings for the global tenants administrator.
- Tenants (TenantSettings[]): list of settings for the application tenants.
IO
By default, the AmbientServices registers the FolderLocationsManager implementation for the service ILocationsManager.
This normalizes the paths and resolves them relative to the application location. Instead, by using IAmbientServices.WithTenantSupport(),
this global service is replaced with TenantFolderLocationsManager which appends the tenant identifier prefixed by dot (.) to each of the folders.
This has the following effects:
- The configuration, licenses, plugins, and other locations dependent on the
ILocationsManagerservice will return different locations for different tenants. - By prefixing the tenant path element with dot (.), this location is considered hidden by other services and, therefore ignored (checked with
location.IsHiddenLocation()extension method).
Changes from Previous Version
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.