How to use a single GraphServiceClient instance in a multitenant app #2893
-
Hello, We have an ASP app that calls Graph API against different tenants. The tenant is programmatically determined by the parameters or JWT claim in the received requests. We do not want to create a new GraphServiceClient every time a request arrives, since it could make dependency injection impossible and unit tests extremely hard (if not totally impossible). So my question is, how can we use a single GraphServiceClient to make calls to different tenants? I found an almost identical issue #838 , but it was raised back in the days of Graph SDK V3 or V4, and the solution doesn't seem applicable to Graph SDK V5. Is there a working solution in V5? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Use the Request configuration to put the tenant in options. (Similar to headers/querystring.) // options can only be identified by type
TenantInfo tenantInfo = <info you have/need regarding the tenant>
await client.something.GetAsync(c=> c.Options.Add(tenantInfo)); In a custom token provider, grab the options and get a token for the desired tenant. async Task AuthenticateRequestAsync(
RequestInformation request,
Dictionary<string, object> additionalAuthenticationContext,
CancellationToken cancellationToken)
{
var tenantInfo = request.GetRequestOption<TenantInfo>();
// use tenantInfo to get correct token
request.Headers.Add("Authorization", $"Bearer {token}");
} |
Beta Was this translation helpful? Give feedback.
Use the Request configuration to put the tenant in options. (Similar to headers/querystring.)
In a custom token provider, grab the options and get a token for the desired tenant.