Base solution for your next web application
Open Closed

How to automatically select the tenant based on unique login credentials (email address only) #6258


User avatar
0
deventerprise created

I have a similar requirement to this: https://support.aspnetzero.com/QA/Questions/977

I do not want the users to select the tenant and neither do I want to provide multiple URL's/domains. Users will register and login with a unique email address (disabling username completely) and select the tenant they are registering for from a drop-down. From the login details you could determine the tenant since it's in the database table, problem is you need the tenant up-front to actually login.

Is there any way to autmatically set the tenant based on a successfull login WITHOUT providing a tenant and using the tenant associated with the user account? When a user registers they will select the tenant from a list on the registration page so there is no need to be on a domain or select it separately for a session.


7 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    you can modify register user and login procedures. just set the tenantId from the dropdown as explained here https://aspnetboilerplate.com/Pages/Documents/Data-Filters#settenantid-method

    and also for the login, find the user by username by disabling tenant filter. https://aspnetboilerplate.com/Pages/Documents/Data-Filters#disable-filters

    and then modify login. set tenantId of the user's tenantId you found from database.

  • User Avatar
    0
    deventerprise created

    Disabling the filters works but the the login fails with an error like "USERNAME already exists". I updated all usernames in the database to match the email address and modified registration to save the email address as username with validation etc.

    When logging in with an updated user, a duplicate user is added to the database somehow with the username "admin" again, and the issue above happens all over again. [email protected] is also added to the database again even though the original user details were modified now having two admins for each tenant.

    Everything else seems to work well, appending null to the tenant cookie fails so tehre is a bug there and you should rather call .Delete.

    How do you prevent these new admin users from being generated?

  • User Avatar
    0
    deventerprise created

    Figured out why the admin users get created, this is because if a user with "admin" does not exist then one is created for you. This happens in TenantRoleAndUserBuilder.cs inside CreateRolesAndUsers. during startup.

    More users are created automatically in HostRoleAndUserCreator.cs inside CreateHostRoleAndUsers, also removed this to avoid it.

  • User Avatar
    0
    alper created
    Support Team

    have you figured out your requirement?

  • User Avatar
    0
    deventerprise created

    I just finished it up now. Was a little tricky to get it right but your advice did help put on the right track as to what I needed to do. Tested everything works in terms of logging in even with the wrong cookie and impersonation.

    Was confused about the logins being created but made some other tweaks there to identify tenant admins a bit better so that I could use phone number user names (off-topic). For anyone else reading, those users are also required for unit tests so don't just remove that code.

  • User Avatar
    0
    deventerprise created

    OK, I cannot seem to register users if they pass in the tenant ID.

    CurrentUnitOfWork.SetTenantId(model.TenantId);
    
    var user = await _userRegistrationManager.RegisterAsync(
        model.Name,
        model.Surname,
        model.EmailAddress,
        model.PhoneNumber,
        model.Password,
        false,
        _appUrlService.CreateEmailActivationUrlFormat(model.TenantId)
    );
    
    user.PhoneNumber = model.PhoneNumber;
    user.ShouldChangePasswordOnNextLogin = false;
    
    await CurrentUnitOfWork.SaveChangesAsync();
    

    I rexpected the above to work, but instead I get an exception saying "InvalidOperationException: Can not register host users!".

    CheckForTenant() in UserRegistrationManager only checks the session, not the unit of work and since this value cannot be set, how do I set in in an unauthorized Register call?

  • User Avatar
    0
    deventerprise created

    Figured out you need to set the Abp.TenantId header: https://support.aspnetzero.com/QA/Questions/6269