Open Closed

[Login][API] Trying to log in from an app using the API - need to know how to identify the tenant that the user belongs to #447


User avatar
0
christianvpernix created

Hi,

We are building an ABP application to serve as the backend and a web portal front end for a multi-tenant solution.

We are also building an iOS mobile app that will utilise the APIs that ABP generates to interact with the backend. The problem we are facing:

  • Identifying how to login a user via the mobile app using the ABP login API without prior knowledge of what tenant that user belongs to.

<br> Ideally what we would like is the ability to identify which tenant the user belongs to driven by their email address. The questions we have are:

  • Is this functionality possible out of the box?
  • Is there another way to achieve this functionality that we are unaware of?
  • Is there a way to custom build a tenant resolver to handle this requirement?

<br> Please let me know what other information you need.

Thanks, Christian

  • ABP Framework version: v3.0.5
  • UI type: MVC
  • Tiered (MVC) or Identity Server Seperated (Angular): no
  • Exception message and stack trace: NA
  • Steps to reproduce the issue: NA

5 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi, You can custom a tenant resolver.

  • User Avatar
    0
    christianvpernix created

    Hi,

    Could you please point us in the direction of some example code on how to achieve this?

    Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi, here is a simple example:

    public class EmailTenantResolveContributor : HttpTenantResolveContributorBase
    {
        public override string Name => "Email";
    
        protected override string GetTenantIdOrNameFromHttpContextOrNull(ITenantResolveContext context,
            HttpContext httpContext)
        {
            if (httpContext.Request.HasFormContentType &&
                httpContext.Request.Path.Value.Contains("Account/Login", StringComparison.InvariantCultureIgnoreCase))
            {
                var email = (string) httpContext.Request.Form["LoginInput.UserNameOrEmailAddress"];
                if (email == null || !email.Contains("@"))
                {
                    return null;
                }
    
                return email.Substring(email.IndexOf('@') + 1).Replace(".com", "");
            }
    
            return null;
        }
    }
    

    Add to your module class:

    Configure<AbpTenantResolveOptions>(options =>
    {
        options.TenantResolvers.Insert(0, new EmailTenantResolveContributor());
    });
    
  • User Avatar
    0
    christianvpernix created

    Hi,

    Thank you for that - I note in the example that you are using the string after the '@' in the email address to identify tenancy.

    What we are trying to do is get the user email address and check it against the database for a matching record and thus a matching tenant. Is this possible?

    A user won't have a tenant email address as they will be using their personal email. In this case your example does not help.

    Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    This is just a simple example,

    Identifying how to login a user via the mobile app using the ABP login API without prior knowledge of what tenant that user belongs to

    It is not possible to log in to the application without knowing the tenant, because the user may be under any one of the tenants and may have the same user name. You need to determine the tenant when logging in.

    You must have some rules for determining the tenant at login.

Made with ❤️ on ABP v9.0.0-preview. Updated on October 07, 2024, 08:52