microsoftgraph / msgraph-sdk-dotnet-auth

Archived - use the TokenCredential classes provided by Azure.Identity. https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme
https://graph.microsoft.com
MIT License
78 stars 19 forks source link

cannot pass account to MS.Graph #102

Closed under3415 closed 3 years ago

under3415 commented 3 years ago

MSAL IdentityClientApp can cache multiple credentials.

I can retrieve these using this code

accounts = await IdentityClientApp.GetAccountsAsync();

I can specify which account to retrieve a token for using this code:

authResult = await IdentityClientApp.AcquireTokenSilent(Scopes, account).ExecuteAsync();

But when I pass IdentityClientApp to MS.Graph DeviceCodeProvider , there is no way to specify which account to use.

DeviceCodeProvider authProvider = new DeviceCodeProvider(IdentityClientApp, Scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

How do I deal with scenarios where multiple credentials are cached? How does graph chose which MSAL credential to use when there are more than one?

AB#8402

under3415 commented 3 years ago

Found the solution. Instead of using MS.Graph.Auth, I am creating Graph Client using this approach:

GraphServiceClient graphClient = new GraphServiceClient(MSGraphURL, new DelegateAuthenticationProvider(async (requestMessage) =>
{
     requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", OutlookToken);
}));