Skip to content

Add New User adapter

Jon P Smith edited this page Jun 15, 2022 · 3 revisions

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.

Available IAddNewUserManager implementations

In version 3.3.0 there are only two implementation of the IAddNewUserManager interface. They are:

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.

A look at the IAddNewUserManager as an authentication handler provider

The IAddNewUserManager has three methods that should be called in order. They are:

1. CheckNoExistingAuthUserAsync

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.

2. SetUserInfoAsync

This is the the main method that will create a authenticate handler and then the AuthP User. There are two ways to do this:

  1. 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.
  2. 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.

3. LoginAsync

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.

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally