Open Closed

TenantId not available in the API #1859


User avatar
0
rick@i-pulse.nl created

We have a custom API implementation so without packages of ABP. We created the clientcredentials and we can get data. But on the API the TenantId is not filled so the filtering is not correct now. What should we add to the request to fill the TenantId in the filtering?

I can add it as request parameter and set the tenantId with CurrentTenant.Change(), but there must be a better way. I also tried to add the TenantId as Claim in the Client settings, but that didn't work.


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi rick

    Can you share some code or more details?

  • User Avatar
    0
    rick@i-pulse.nl created

    hi rick

    Can you share some code or more details?

    We generated the client from the REST API by swagger. This is a C#.NET client of the gateway in the Microservice solution.

    This code will not help to solve this. I have to know how we can set the TenantId on the client so it will automaticly filter in the API. Of can we add a setting in the client credential section in the portal?

    We can do a Teams call so I can share my screen and you can help me?!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    There are many ways to set the requested tenant. Please note that CurrentUserTenantResolveContributor should always be the first contributor for the security.

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#default-tenant-resolvers

  • User Avatar
    0
    rick@i-pulse.nl created

    I was just trying to get that TenantId in our ProductService. We are running on a NGINX server so I want to try another key then “__tenantId”.

    Our product service solution looks like this (Microservice):

    1. In the IBP.ProductService.HttpAPI I added this in the ‘ProductServiceHttpApiModule’:
    2. I also added the CustomTenantResolver:
    3. When I call the API, I got this error:
    4. I filled the tenantId in the querystring and in the requestHeader, but both are not used/found. See data:

    Questions

    • Can you see what is going wrong?
    • Is the CustomerTenantResolver added in the correct project?
    • I can create this workaround, but that's not the right way!
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Did you changed the TenantKey ?

    services.Configure<AbpAspNetCoreMultiTenancyOptions>(options =>
    {
        options.TenantKey = "__tenantId";
    });
    
  • User Avatar
    0
    rick@i-pulse.nl created

    No. In which project do we need this?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to add it in HttpApi.Host.

  • User Avatar
    0
    rick@i-pulse.nl created

    I added this and now I got this error (same as in my original message). I filled the tenantId in the header and querystring but both are not recognized.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you check the logs to see the tenant name or id that not found?

  • User Avatar
    0
    rick@i-pulse.nl created

    Can we do a Teams call to check this together instead of these tickets? Please send me an email to get in touch.

    I shared my logging above and there is no other logging about tenantId or name.

  • User Avatar
    0
    alper created
    Support Team Director

    Hi rick,

    please keep the conversation here.

  • User Avatar
    0
    rick@i-pulse.nl created

    Hi rick,

    please keep the conversation here.

    That's okay for me but my problem is not getting solved. Most of the times it helps to share a screen :).

  • User Avatar
    0
    cotur created

    Hi,

    ABP Applications uses a Middlewre to determine the tenant.

    This middleware uses different contributor for trying to find tenant id by given order. When it finds a tenant id, the rest of contributors won't be executed.

    1. CurrentUserTenantResolveContributor > This should always be the first contributor for the security.
    2. QueryStringTenantResolveContributor
    3. FormTenantResolveContributor
    4. RouteTenantResolveContributor
    5. HeaderTenantResolveContributor
    6. CookieTenantResolveContributor

    If the CurrentUserTenantResolveContributor returns the tenantId, then the QueryStringTenantResolveContributor will not be executed.

    This contributors are checkin the parameter that defined in the AbpAspNetCoreMultiTenancyOptions for the tenant id.

    You can change it by configuring it as @maliming defined before.

    services.Configure<AbpAspNetCoreMultiTenancyOptions>(options =>
    {
        options.TenantKey = "MyTenantKey";
    });
    

    The whole tenant resolving works as defined in the documentation https://docs.abp.io/en/abp/latest/Multi-Tenancy#default-tenant-resolvers

    How you achieve your problem;

    1. You may change the order of resolvers. As I understand, you want to make some changes for other tenant that the user does not related.
    2. You may add your custom tenant resolver. https://docs.abp.io/en/abp/latest/Multi-Tenancy#custom-tenant-resolvers

    If you want to make your custom tenant resolver the first executed one, don't forget the change resolver orders.

    Regards.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11