Open Closed

SocialLogin for more than one tenant #541


0
byteplatz created

Hello Halil,

I managed to make google auth work for aspnetzero and I was wondering if you can help me to understand if its possible to use same google login for diferente tenants ...

After logging with the first tenant (default for example), every time I click on G+ login icon it automatticaly signin into default tenant.

Is this possible?

If so I guess it will only work with Tenant detection using URL ?

Bruno


6 Answer(s)
  • 0
    hikalkan created
    Support Team

    Hi,

    You can change behaviour for your application. This logic is located here: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Controllers/AccountController.cs#L619">https://github.com/aspnetzero/aspnet-ze ... er.cs#L619</a>

    var tenants = await FindPossibleTenantsOfUserAsync(loginInfo.Login);
                        switch (tenants.Count)
                        {
                            case 0:
                                return await RegisterView(loginInfo);
                            case 1:
                                tenancyName = tenants[0].TenancyName;
                                break;
                            default:
                                return View("TenantSelection", new TenantSelectionViewModel
                                {
                                    Action = Url.Action("ExternalLoginCallback", "Account", new { returnUrl }),
                                    Tenants = tenants.MapTo<List<TenantSelectionViewModel.TenantInfo>>()
                                });
                        }
    

    As you see, it goes to register if no tenant registration before. It logins it only one tenant. It tries to select tenant if more than one tenant.

    So, you can send your user to "TenantSelection" view even if he is registered to only one tenant. In this page, you can add a 'register' button to redirect to register view and so on... Your implementation can be different, surely.

    Have a nice day.

  • 0
    byteplatz created

    I will need to investigate why the tenancy name is not being set in the callback url. ..

    Maybe the controller action need some adjustment...

    It is always null on ExternalLoginCallback even when using url tenant detection

    I will let you know

  • 0
    byteplatz created

    Do you have any other code besides Startup.cs and AccountController.cs for external authentication ?

    Bruno

  • 0
    hikalkan created
    Support Team

    No, all code should be there.

  • 0
    byteplatz created

    Thank you !

    I will try to implement a login flow without field for tenancy name (using tenant detection by username on Login action)...

    Quick question : Why there are no method to FindAllAsync (Users) and only exists for (External) Logins ?

    [UnitOfWork]
            public virtual Task<List<TUser>> FindAllAsync(UserLoginInfo login)
            {
                var query = from userLogin in _userLoginRepository.GetAll()
                            join user in _userRepository.GetAll() on userLogin.UserId equals user.Id
                            where userLogin.LoginProvider == login.LoginProvider && userLogin.ProviderKey == login.ProviderKey
                            select user;
    
                return Task.FromResult(query.ToList());
            }
    
    public virtual Task<TUser> FindAsync(int? tenantId, UserLoginInfo login)
            {
                var query = from userLogin in _userLoginRepository.GetAll()
                    join user in _userRepository.GetAll() on userLogin.UserId equals user.Id
                    where user.TenantId == tenantId && userLogin.LoginProvider == login.LoginProvider && userLogin.ProviderKey == login.ProviderKey
                    select user;
    
                return Task.FromResult(query.FirstOrDefault());
            }
    

    They both go only in LoginRepository...Are there any specific reasons/arthicteture? Or it is just a matter of "not implemented" ?

    Can I implement those methods for Users in UserManager.cs from my application ? Will that interfere with UoW or AuditLogs ?

    Bruno

  • 0
    hikalkan created
    Support Team

    You can use UserManager.Users which returns IQueryable to quesy users. So, UserManager.Users.Tolist() gets all users, for example.