-
Notifications
You must be signed in to change notification settings - Fork 162
Add New User adapter
To make a generic version of the Invite new user service and Sign up for a new tenant, with versioning features I needed a generic service to handle the adding of a new user to the application. I therefore created a interface called IAddNewUserManager
which can provide a common add user service that (potentially) can work with any ASP.NET Core authentication handler.
In version 3.3.0 there are only two implementation of the IAddNewUserManager
interface. They are:
-
IndividualUserAddUserManager<TIdentity>
which works with the individual user accounts authentication handler. You can find an example using this implementation in Example 3 with both Invite new user service and Sign up for a new tenant, with versioning features. -
AzureAdUserManager
which works with Azure AD authentication handler. You can find an example using this implementation in Example 5 for the Invite new user service feature. (NOTE: won't work with Azure AD B2C with social logins).
NOTE: More implementations may be added, or you can build your own by implementing the IAddNewUserManager
interface.
If you are using the "Invite new user" or "Sign up / with versioning" features you need to manually, e.g.
services.AddTransient<IAddNewUserManager, AzureAdUserManager>()
services.AddTransient<IInviteNewUserService, InviteNewUserService>();
NOTE: Typically you register a single implementation of the IAddNewUserManager
. It is possible to support multiple authentication handlers, but I haven't shown how to do that. Create an issue if you want to support multiple authentication handlers at the same time.
The IAddNewUserManager
has three methods that should be called in order. They are:
This is a simple test to check that there isn't an existing AuthP User with the same email (or username depending on the authenticate handler). If an AuthP User matches the new user's email / username then it returns and error which stops the whole process, which saves the "Invite new user" or "Sign up / with versioning" code from undoing things just because wasn't valid.
This is the the main method that will create a authenticate handler and then the AuthP User. There are two ways to do this:
- If the authentication handler supports a "find user" and "create user" features, then it can find and/or create a new authentication handler user. Once that is done, then you can use the authentication handler user's UserId to create a AuthP User, including AuthP parts such as Roles, Tenants, Sharding etc.
- If the authentication handler doesn't support the "find user" and "create user" features, then it has to store the email and AuthP User data in the database and when the user logs in it will intercept the login and create the AuthP User.
The individual user accounts and Azure AD authentication handlers both support the "find user" and "create user" features, so they use approach 1. Approach 2 is useful for social authentication handlers like Google, Twitter etc.
This either logs in the new user (individual user accounts uses this approach), or in the case of Azure AD authentication handler it returns a temporary password for the first login where they are asked to reset the (temporary) password to a password from the newly create user.
- 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