Base solution for your next web application
Open Closed

AUTOMATICALLY DETECT TENANT ON LOGIN #9608


User avatar
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

28 Answer(s)
  • User Avatar
    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.

  • User Avatar
    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

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

  • User Avatar
    1
    alfar_re created

    Hi @ismcagdas,

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

  • User Avatar
    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

  • User Avatar
    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

            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.

  • User Avatar
    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);`

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can switch to host using below syntax;

    
    using (_unitOfWorkManager.Current.SetTenantId(null))
            {
                // your code...
            }
    
  • User Avatar
    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?

  • User Avatar
    0
    alfar_re created

    Hi,

    I'm stuck with this. Any help?

  • User Avatar
    0
    alfar_re created

    Still stuck. I need urgent assistance...

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Could you share your final Login action ?

    Thanks,

  • User Avatar
    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 });
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Is it possible to share your project via email with [email protected] ?

    Thanks,

  • User Avatar
    0
    alfar_re created

    Hi,

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

  • User Avatar
    0
    alfar_re created

    Hi @ismcagdas,

    Any help will be appreciated. I'm really stuck on this one.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

    Sorry, I couldn't see your email. Could you share the email subject and name of the project ?

    Thanks,

  • User Avatar
    0
    alfar_re created

    Hi @ismcagdas,

    The email subject is the same as the title of this issue

    AUTOMATICALLY DETECT TENANT ON LOGIN #9608

  • User Avatar
    0
    alfar_re created

    Hi @ismcagdas,

    Any assistance with this issue will be highly appreciated. This issue is affecting a deliverable.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

    It seems like the project link is expired. Could you share it again ? I will check this today.

    Thanks,

  • User Avatar
    0
    alfar_re created

    Hi **@ismcagdas **,

    I've reshared the link on mail with the same subject. Kindly check it.

    Thank you.

  • User Avatar
    0
    alfar_re created

    Hi @ismcagdas,

    Kindly confirm receipt of the email. I need to close this matter by tomorrow since there are a few development tasks being delayed by it. Please. Thank you.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We got the project now, thanks.

  • User Avatar
    0
    alfar_re created

    Hi,

    Great. Hoping for a resolution soon.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @alfar_re

    I have replied via email. We can continue with email conversation.

  • User Avatar
    0
    alfar_re created

    Hi @ismcagdas,

    Great. Thank you.

  • User Avatar
    0
    CrayonGroupSCA created

    Would it be possible to post the final solution here so that others (including myself) could benefit from it?

  • User Avatar
    0
    alfar_re created

    Yes, sure.

    So, I created a new action and called it AdminLogin. This is used by the admins only. The default login has a modified login method that detects tenant users automatically.