Open Closed

AUTOMATICALLY DETECT TENANT ON LOGIN #9608


0
alfar_re created

Hi,

How can I detect the current user/ tenant from the login page without using the tenant selection or subdomain?

  • ASP.NET
  • MVC
  • .NET CORE
  • Latest ABP Framework according to Zero 9.1

15 Answer(s)
  • 0
    zony created
    Support Team

    Hi alfar_re, At present, if you want to select tenants without perception, the best way is to distinguish by subdomain. Because the user cannot know which tenant the current user belongs to when logging in for the first time. Perhaps you can add a unique prefix to the username when creating a user, rewrite a tenant resolver, and select tenants based on the prefix.

  • 0
    alfar_re created

    Hi @zony,

    I see. The good thing is that the system we are building only allows one user per tenant and the username is a mobile number which is unique. That means that you will only have one tenant with one mobile number. That way it'll be easy to identify the tenants.

  • 0
    ismcagdas created
    Support Team

    Hi @alfar_re

    You can also query AbpUserAccounts table if you don't want to use subdomains.

  • 1
    alfar_re created

    Hi @ismcagdas,

    I see. I will try this out and let you know.

  • 0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Just don't forget to switch to HOST side while querying this table, see https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants

  • 0
    alfar_re created

    Hi,

    Gone through the login logic. Not sure I fully understand how it functions but I modified this line at line 180 > AccountsController

    var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, await GetTenancyNameFromUsernameAsync(loginModel.UsernameOrEmailAddress));

    Then reused the code like below;

    `private async Task<string> GetTenancyNameFromUsernameAsync(string userName) { var isTenantAvailable = await _accountAppService.IsTenantAvailable(new IsTenantAvailableInput { TenancyName = userName });

            if (isTenantAvailable != null) { return isTenantAvailable.TenantId.ToString(); }
            else { throw new InvalidOperationException($"Given user ({userName}) could not be found!"); }
        }`
       
    

    Let me know if this is the right direction or point me to the right direction. Thanks.

  • 0
    alfar_re created

    Hi,

    Still haven't gotten a response on this. I made some changes and it worked for the tenant and I'd like to know how or where I switch to host.

    `var usr = await _accountAppService.IsTenantAvailable(new IsTenantAvailableInput { TenancyName = loginModel.UsernameOrEmailAddress });

    var userTenantId = usr.TenantId;

    var tenancyNameOrNull = userTenantId.HasValue ? _tenantCache.GetOrNull((int)userTenantId)?.TenancyName : null;

    var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, tenancyNameOrNull);`

  • 0
    ismcagdas created
    Support Team

    Hi,

    You can switch to host using below syntax;

    
    using (_unitOfWorkManager.Current.SetTenantId(null))
            {
                // your code...
            }
    
  • 0
    alfar_re created

    Hi,

    I successfully detected the admin login but after logging in I get this error

    Current user did not login to the application!

    What am I missing?

  • 0
    alfar_re created

    Hi,

    I'm stuck with this. Any help?

  • 0
    alfar_re created

    Still stuck. I need urgent assistance...

  • 0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Could you share your final Login action ?

    Thanks,

  • 0
    alfar_re created

    Hi @ismcagdas

    Here you go.

    public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "", string ss = "") 
            {
                returnUrl = NormalizeReturnUrl(returnUrl);
                if (!string.IsNullOrWhiteSpace(returnUrlHash))
                {
                    returnUrl = returnUrl + returnUrlHash;
                }
    
                if (UseCaptchaOnLogin())
                {
                    await _recaptchaValidator.ValidateAsync(HttpContext.Request.Form[RecaptchaValidator.RecaptchaResponseKey]);
                }
    
                var usr = await _accountAppService.IsTenantAvailable(new IsTenantAvailableInput
                {
                    TenancyName = loginModel.UsernameOrEmailAddress
                });
    
                var userTenantId = usr.TenantId;
    
                var tenancyNameOrNull = userTenantId.HasValue ? _tenantCache.GetOrNull((int)userTenantId)?.TenancyName : null;
    
                var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, tenancyNameOrNull);
                //
                if (!string.IsNullOrEmpty(ss) && ss.Equals("true", StringComparison.OrdinalIgnoreCase) && loginResult.Result == AbpLoginResultType.Success)
                {
                    loginResult.User.SetSignInToken();
                    returnUrl = AddSingleSignInParametersToReturnUrl(returnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId);
                }
    
                if (_settingManager.GetSettingValue<bool>(AppSettings.UserManagement.AllowOneConcurrentLoginPerUser))
                {
                    await _userManager.UpdateSecurityStampAsync(loginResult.User);
                }
    
                if (loginResult.User.ShouldChangePasswordOnNextLogin)
                {
                    loginResult.User.SetNewPasswordResetCode();
    
                    return Json(new AjaxResponse
                    {
                        TargetUrl = Url.Action(
                            "ResetPassword",
                            new ResetPasswordViewModel
                            {
                                TenantId = AbpSession.TenantId,
                                UserId = loginResult.User.Id,
                                ResetCode = loginResult.User.PasswordResetCode,
                                ReturnUrl = returnUrl,
                                SingleSignIn = ss
                            })
                    });
                }
    
                var signInResult = await _signInManager.SignInOrTwoFactorAsync(loginResult, loginModel.RememberMe);
                if (signInResult.RequiresTwoFactor)
                {
                    return Json(new AjaxResponse
                    {
                        TargetUrl = Url.Action(
                            "SendSecurityCode",
                            new
                            {
                                returnUrl = returnUrl,
                                rememberMe = loginModel.RememberMe
                            })
                    });
                }
    
                Debug.Assert(signInResult.Succeeded);
    
                await UnitOfWorkManager.Current.SaveChangesAsync();
    
                return Json(new AjaxResponse { TargetUrl = returnUrl });
            }
    
  • 0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Is it possible to share your project via email with info@aspnetzero.com ?

    Thanks,

  • 0
    alfar_re created

    Hi,

    I shared the project with you. I'm really stuck and would appreciate a resolution. Thanks.