Base solution for your next web application
Open Closed

Auto Detect Tenant #9306


User avatar
0
-bitman created

Hello ANZ Team,

I am in the process of writing a native mobile app and I would like to get some advice on how to detect the tenant id based on just the username and password entered by the user? I understand that when authenticating a user, I need to pass the tenant id along with it but since my app will be used by different tenants I don't want to expose them to each other. Do you have a recommended solution to this situation?

Looking forward to your response.

Thank you very much!


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @-bitman,

    You can try to query AbpUserAccounts table and if there is a 1 match with entered username, you can use it's TenantId.

  • User Avatar
    0
    -bitman created

    I figured this will be the recommended approach but given we implement a separate physical database per tenant, that would mean we would have to query across all databases just to figure this out. Not to mention that usernames are only unique per tenant instead of globally.

    Do you have another recommendation @ismcsgdas? Aside from using only a single database.

    Thank you!

  • User Avatar
    0
    gterdem created
    Support Team

    Hello @-bitman, it is like trying a key-value pair across different files/dbs. I don't think you can magically change the process.

    However, you can create something like AppUserTenantService keeping unique username as key and tenantId as value in a cache (even on redis) so you can speed up the process in a large margin by asking to cache which tanenant does the username belong.

    Though it will require some extra work (like adding new registered users to cache, invalidating inactive users etc) but would worth the extra work if you are planning your application in a large scale.

    I hope it was helpful.

  • User Avatar
    0
    -bitman created

    Thank you for your guidance @gterdem. Will look into this some more. Thank you.