Open Closed

Hide Tenant Selection in the login screen (MVC) #9755


User avatar
0
AhmedMohamed92 created

How to Hide Tenant Selection in the login screen & handles tenant resolving in the backend by username


5 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    To hide the tenant selection on the login screen and resolve the tenant by username in the backend for an ABP Angular UI (v9.0.4, non-tiered), follow these steps:

    1. Remove Tenant Selection from the Login UI

      • In Angular, the tenant switch is typically shown based on the multi-tenancy configuration. You can hide the tenant input by customizing the login component template to remove or hide the tenant field.
    2. Backend: Resolve Tenant by Username

      • Override the login logic on the backend to determine the tenant based on the username or email address provided. This involves:
        • Searching for the user across all tenants.
        • Once the user is found, set the current tenant context before proceeding with authentication.

      Example implementation (in your AccountController or a custom login service):

    public class CustomLoginModel : LoginModel
       {
           private readonly ITenantRepository _tenantRepository;
    
           public CustomLoginModel(
               IAuthenticationSchemeProvider schemeProvider,
               IOptions<AbpAccountOptions> accountOptions,
               IOptions<IdentityOptions> identityOptions,
               ITenantRepository tenantRepository,
               IdentityDynamicClaimsPrincipalContributorCache contributorCache)
               : base(schemeProvider, accountOptions, identityOptions, contributorCache)
           {
               _tenantRepository = tenantRepository;
           }
    
           public override async Task<IActionResult> OnPostAsync(string action)
           {
               var user = await FindUserAsync(LoginInput.UserNameOrEmailAddress);
               using (CurrentTenant.Change(user?.TenantId))
               {
                   return await base.OnPostAsync(action);
               }
           }
    
           protected virtual async Task<IdentityUser> FindUserAsync(string uniqueUserNameOrEmailAddress)
           {
               IdentityUser user = null;
               using (CurrentTenant.Change(null))
               {
                   user = await UserManager.FindByNameAsync(LoginInput.UserNameOrEmailAddress) ??
                          await UserManager.FindByEmailAsync(LoginInput.UserNameOrEmailAddress);
    
                   if (user != null)
                   {
                       return user;
                   }
               }
    
               foreach (var tenant in await _tenantRepository.GetListAsync())
               {
                   using (CurrentTenant.Change(tenant.Id))
                   {
                       user = await UserManager.FindByNameAsync(LoginInput.UserNameOrEmailAddress) ??
                              await UserManager.FindByEmailAsync(LoginInput.UserNameOrEmailAddress);
    
                       if (user != null)
                       {
                           return user;
                       }
                   }
               }
               return null;
           }
       }
    
    • This code searches for the user in the host and all tenants, and sets the tenant context accordingly.
    1. Ensure Usernames/Emails are Unique Across Tenants

      • This approach requires that usernames or emails are unique across all tenants.
    2. No Tenant Input on Frontend

      • The Angular frontend should only ask for username/email and password.

    With this setup, users log in with just their username/email and password, and the backend determines the tenant automatically.

    Sources:


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    AhmedMohamed92 created

    my login page is MVC, the auth flow is codeflow when user try to login angular app redirect to Login Page MVC on backend side

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    1.

    Removing QueryStringTenantResolveContributor and CookieTenantResolveContributor will hide the tenant selection in the login page.

    Configure<AbpTenantResolveOptions>(options =>
    {
        options.TenantResolvers.RemoveAll(resolver =>
            resolver is QueryStringTenantResolveContributor or CookieTenantResolveContributor);
    });
    

    2.

    If you don't want to remove them, you can override the MVC theme account layout page.

    see https://github.com/abpframework/abp/blob/dev/modules/basic-theme/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Themes/Basic/Layouts/Account.cshtml#L70-L72

    Thanks.

  • User Avatar
    0
    AhmedMohamed92 created

    Configure<AbpTenantResolveOptions>(options => { options.TenantResolvers.RemoveAll(resolver => resolver is QueryStringTenantResolveContributor or CookieTenantResolveContributor); }); Hide them but how tenant will be resolved for the loggedIn user should i create custom tenant resolver or should i create custom login to customize login process ?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    should i create custom tenant resolver or should i create custom login to customize login process ?

    Both are ok.

    You can switch the tenant by override the Login page.

    Or add a new MyTenantResolveContributor, and try to get request info to determine the current tenant.

    Thanks,

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 12, 2025, 10:20