Skip to content

How to use a single GraphServiceClient instance in a multitenant app #2893

Answered by pschaeflein
GeorgiusLing asked this question in Q&A
Discussion options

You must be logged in to vote

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}");
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@GeorgiusLing
Comment options

Answer selected by GeorgiusLing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants