-
Notifications
You must be signed in to change notification settings - Fork 164
Sign up for a new tenant, with versioning
Version 3.3.0 of the AuthP library provides a service that allows an external user to sign up for a new tenant. This service also contains an optional feature called versioning where you can provide tenants with different features (and prices). This allows an new user to:
- Create a new tenant for their company / organisation
- If your application uses versioning, the the new user can choose (and pay) the version they suite them
- The new user is registered to your application and is linked to new tenant
- And optionally the new user can designated as an tenant admin, which allows that user to manage the users within their tenant
This makes it easy for anyone sign up to your application - this type of approach is called self-service provisioning.
The "sign up / versioning" feature uses two services in the AuthPermissions.SupportCode namespace.
- The
ISignInAndCreateTenant\SignInAndCreateTenant, which implements the “sign up / versioning” service. - The
IAuthenticationAddUserManagerservice which theSignInAndCreateTenantrelies on for adding a new user - !!! - And if you Sharding turned on, then you need to register your implementation of the
IGetDatabaseForNewTenantinterface.
Any code in the AuthPermissions.SupportCode project has to be manually, and the code shown below is taken from Example 3 during the registering of the service to use in your ASP.NET Core application.
//Add the SupportCode services
services.AddTransient<IAuthenticationAddUserManager, IndividualUserAddUserManager<IdentityUser>>();
services.AddTransient<ISignInAndCreateTenant, SignInAndCreateTenant>();
//If Sharding is turned on then include the following registration
services.AddTransient<IGetDatabaseForNewTenant, YourVersionOfThisService>();Note that the IAuthenticationAddUserManager service uses an implementation called IndividualUserAddUserManager<IdentityUser> which works with applications using the individual user accounts authenticate provider. Version 3.3.0 only contains two implementations of the IAuthenticationAddUserManager interface. They are:
-
IndividualUserAddUserManager<TIdentity>which works with the individual user accounts authenticate provider. -
AzureAdUserManagerwhich works with Azure AD authenticate provider _(NOTE: won't work with Azure AD B2C with social logins).
More implementations may be added, or you can build your own by implementing the IAuthenticationAddUserManager interface.
If you want to use the versioning feature you need to create a MultiTenantVersionData class containing the different features for each version. There are three parts to the versioning:
-
TenantRolesForEachVersionthat defines the Tenant Roles which will add extra features to the different versions. -
TenantAdminRoleswhich defines the Roles that the new user should have - this allows you to decide if the users gets admin Roles. -
HasOwnDbForEachVersionis required if Sharding is turned on. It allows you to define whether the tenant will have its own database (i.e. Sharding) or are in a database with other tenants.
Each of these properties are a Dictionary, where the string Key holds the name of the version. The code below comes from Example 3
public static class Example3CreateTenantVersions
{
public static readonly MultiTenantVersionData TenantSetupData = new()
{
TenantRolesForEachVersion = new Dictionary<string, List<string>>()
{
{ "Free", null },
{ "Pro", new List<string> { "Tenant Admin" } },
{ "Enterprise", new List<string> { "Tenant Admin", "Enterprise" } },
},
TenantAdminRoles = new Dictionary<string, List<string>>()
{
{ "Free", new List<string> { "Invoice Reader", "Invoice Creator" } },
{ "Pro", new List<string> { "Invoice Reader", "Invoice Creator", "Tenant Admin" } },
{ "Enterprise", new List<string> { "Invoice Reader", "Invoice Creator", "Tenant Admin" } }
}
//No settings for HasOwnDbForEachVersion as Example3 isn't using sharding
};
}You need an action / page which can be accessed by a user who isn't logged in to sign up for a new tenant - see Example 3 with the "Sign up now!" link on the Home navbar. The data your will need are:
- The email of the new user (optionally their UserName too)
- The name they want for the tenant
- Depending on which authentication provider you selected (see AuthenticationAddUserManager service) you might need the password the new user wants to use.
The SignInAndCreateTenant service has two methods, one for no versioning and one that uses versioning. The two subsections describe how to use each approach.
In this case you use the SignUpNewTenantAsync(AddNewUserDto newUser, AddNewTenantDto tenantData) method which takes in:
-
newUserwhich contains the new user's information, e.g. email, username, password, plus the Roles that the new user should have. -
tenantDatawhich should contain theTenantNamefor the new tenant. If Sharding is turned on you must also provide set theHasOwnDbproperty to true or false and theRegionproperty can be used if you have geographically spread out database servers and is used byIGetDatabaseForNewTenantservice to pick the correct database server location.
In this case you use the SignUpNewTenantWithVersionAsync(AddNewUserDto newUser, AddNewTenantDto tenantData, MultiTenantVersionData versionData) method which takes in:
-
newUserwhich contains the new user's information, e.g. email, username, password, plus the Roles that the new user should have. -
tenantDatawhich should contain theTenantNamefor the new tenant and theVersionproperty should contain a valid version name. If Sharding is turned on and your database servers are geographically spread out, then you can set theRegionproperty which will be used byIGetDatabaseForNewTenantservice to pick the correct database server location. -
versionDatawhich defines the configuration of the tenant (seeExample3CreateTenantVersionsexample shown earlier in this document).
Once the new tenant is created the new user is registered as a valid user for the new tenant. Then the new user is logged in.
NOTE: If the registering of the new user fails the new tenant is deleted so that the user can try again and the tenant name is still available to them.
- Add New User service
- Get new sharding database
- 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