首页 > 解决方案 > 如何使用 Microsoft Graph 服务客户端选择缓存凭据

问题描述

我有 MSAL 身份客户端应用程序,可用于获取用户的访问令牌。

我现在正尝试使用它Microsoft.Graph.Auth来发送请求。下面的代码显示了如何将 MSALIndentityClientApp传递给Graph.Auth.

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

如果缓存了多个凭据,我无法确定如何指定要使用的缓存凭据。

当我单独使用 MSAL 时,我使用下面的代码来指定用户帐户。如何使用 MS.Graph 做到这一点?

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

标签: c#microsoft-graph-apimsal

解决方案


使用这种方法创建GraphClient允许我控制使用的帐户:

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

资源


推荐阅读